Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

モックデータを追加 #16

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions back/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Config_t struct {
PostgresHost string `env:"POSTGRES_HOST"`
PostgresPort string `env:"POSTGRES_PORT"`

ISDebugMode string `env:"IS_DEBUG_MODE"`

ServerPort string
}

Expand Down
2 changes: 1 addition & 1 deletion back/domain/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d *dbImpl) GetPayment(id string) (Payment, error) {
// GetPaymentsByMoneyPoolID retrieves all payments associated with a specific money pool.
func (d *dbImpl) GetPaymentsByMoneyPoolID(moneyPoolID string) ([]Payment, error) {
var payments []Payment
query := `SELECT id, money_pool_id, date, title, amount, description, is_planned, store_id FROM payment WHERE money_pool_id = $1`
query := `SELECT id, money_pool_id, date, title, amount, description, is_planned, store_id FROM payment WHERE money_pool_id = $1 ORDER BY date DESC`
err := d.db.Select(&payments, query, moneyPoolID)
if err != nil {
return nil, fmt.Errorf("error fetching payments: %v", err)
Expand Down
11 changes: 11 additions & 0 deletions back/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/walnuts1018/openchokin/back/config"
"github.com/walnuts1018/openchokin/back/usecase"
)

var (
uc *usecase.Usecase
)

func UserMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("userID", "1")
c.Next()
}
}
func NewHandler(usecase *usecase.Usecase) (*gin.Engine, error) {
uc = usecase
r := gin.Default()
if config.Config.ISDebugMode == "true" {
r.Use(UserMiddleware())
}

v1 := r.Group("/v1")
{
// クエリパラメータtype=summary or detailでサマリーと詳細を分けられる。
Expand Down
64 changes: 64 additions & 0 deletions back/infra/psql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,67 @@ CREATE TABLE IF NOT EXISTS restricted_publication_scope (
ALTER TABLE restricted_publication_scope
ADD CONSTRAINT fk_money_pool_restricted
CHECK ((SELECT type FROM money_pool WHERE id = pool_id) = 'restricted');

-- 初期データ追加
-- usersテーブルにデータを挿入
INSERT INTO users (id) VALUES (DEFAULT), (DEFAULT), (DEFAULT);

-- user_groupsテーブルにデータを挿入
INSERT INTO user_groups (name, creator_id) VALUES
('Developers Group', 1),
('Designers Group', 1),
('Managers Group', 2);

-- money_poolテーブルにデータを挿入
INSERT INTO money_pool (name, description, type, owner_id) VALUES
('Fund for Emergencies', 'Emergency funds for unexpected expenses', 'private', 1),
('Office Party Fund', 'Savings for annual office parties', 'public', 2),
('Project X Budget', 'Budget allocated for Project X', 'restricted', 3);

-- money_providerテーブルにデータを挿入
INSERT INTO money_provider (name, creator_id, balance) VALUES
('John’s Wallet', 1, 1000.0000),
('Anna’s Savings', 2, 1500.0000),
('Company Petty Cash', 3, 500.0000);

-- storeテーブルにデータを挿入
INSERT INTO store (name, creator_id) VALUES
('Tech Gadgets', 1),
('Office Supplies Store', 2),
('Bookstore', 3);

-- itemテーブルにデータを挿入
INSERT INTO item (name, creator_id) VALUES
('Laptop', 1),
('Ergonomic Keyboard', 1),
('Planner Notebook', 2);

-- labelテーブルにデータを挿入
INSERT INTO label (name, creator_id) VALUES
('Electronics', 1),
('Office Equipment', 1),
('Stationery', 2);

-- paymentテーブルにデータを挿入
INSERT INTO payment (money_pool_id, date, title, amount, description, is_planned, store_id) VALUES
(1, '2023-01-15', 'Emergency Repair', 200.0000, 'Repairing office printer', TRUE, NULL),
(2, '2023-02-20', 'Office Party', 300.0000, 'Annual office party expenses', FALSE, NULL),
(3, '2023-03-12', 'Project X Software', 400.0000, 'Software purchase for Project X', TRUE, 1);

-- item_paymentテーブルにデータを挿入
INSERT INTO item_payment (payment_id, item_id, quantity) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3);

-- user_group_membershipテーブルにデータを挿入
INSERT INTO user_group_membership (group_id, user_id) VALUES
(1, 1),
(1, 2),
(2, 3);

-- restricted_publication_scopeテーブルにデータを挿入
INSERT INTO restricted_publication_scope (pool_id, group_id) VALUES
(3, 1),
(3, 2),
(3, 3);
21 changes: 20 additions & 1 deletion back/infra/psql/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ const (
)

func dbInit() error {
db, err := sqlx.Open("postgres", fmt.Sprintf("host=%v port=%v user=%v password=%v sslmode=%v", config.Config.PostgresHost, config.Config.PostgresPort, config.Config.PostgresAdminUser, config.Config.PostgresAdminPassword, sslmode))
db, err := sqlx.Open("postgres", fmt.Sprintf("host=%v port=%v user=%v password=%v sslmode=%v",
config.Config.PostgresHost, config.Config.PostgresPort, config.Config.PostgresAdminUser, config.Config.PostgresAdminPassword, sslmode))
if err != nil {
return fmt.Errorf("failed to open db: %w", err)
}
defer db.Close()

// Check if the user exists
var roleName string
err = db.Get(&roleName, "SELECT rolname FROM pg_roles WHERE rolname = $1", config.Config.PostgresUser)
if err != nil && err != sql.ErrNoRows {
return fmt.Errorf("error checking for user existence: %w", err)
}

// If the user does not exist, create it
if roleName == "" {
// Create a new user with a password
_, err = db.Exec(fmt.Sprintf("CREATE USER %v WITH PASSWORD '%v'",
config.Config.PostgresUser, config.Config.PostgresPassword))
if err != nil {
return fmt.Errorf("failed to create user: %w", err)
}
}

// Check if the database exists
var dbName string
err = db.Get(&dbName, "SELECT datname FROM pg_database WHERE datname = $1", config.Config.PostgresDb)
if err != nil && err != sql.ErrNoRows {
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
POSTGRES_DB: chokindb
POSTGRES_HOST: openchokin-db
POSTGRES_PORT: 5432
IS_DEBUG_MODE: true
volumes:
- type: bind
source: "./back"
Expand Down