diff --git a/.gitignore b/.gitignore index e80176d..32a2000 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -# bin -# coverage.out -# coverage -# .env -# tmp -# build -# *.log \ No newline at end of file +bin +coverage.out +coverage +.env +tmp +build +*.log \ No newline at end of file diff --git a/db/migrations/000006_recommendation.up.sql b/db/migrations/000006_recommendation.up.sql index f7806c5..28d4cc0 100644 --- a/db/migrations/000006_recommendation.up.sql +++ b/db/migrations/000006_recommendation.up.sql @@ -1,18 +1,18 @@ --- CREATE TABLE virtual_games ( --- id BIGSERIAL PRIMARY KEY, --- name VARCHAR(255) NOT NULL, --- provider VARCHAR(100) NOT NULL, --- category VARCHAR(100) NOT NULL, --- min_bet DECIMAL(15,2) NOT NULL, --- max_bet DECIMAL(15,2) NOT NULL, --- volatility VARCHAR(50) NOT NULL, --- rtp DECIMAL(5,2) NOT NULL, --- is_featured BOOLEAN DEFAULT false, --- popularity_score INTEGER DEFAULT 0, --- thumbnail_url TEXT, --- created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, --- updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP --- ); +CREATE TABLE IF NOT EXISTS virtual_games ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + provider VARCHAR(100) NOT NULL, + category VARCHAR(100) NOT NULL, + min_bet DECIMAL(15,2) NOT NULL, + max_bet DECIMAL(15,2) NOT NULL, + volatility VARCHAR(50) NOT NULL, + rtp DECIMAL(5,2) NOT NULL, + is_featured BOOLEAN DEFAULT false, + popularity_score INTEGER DEFAULT 0, + thumbnail_url TEXT, + created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP +); CREATE TABLE user_game_interactions ( id BIGSERIAL PRIMARY KEY, diff --git a/db/query/branch.sql b/db/query/branch.sql index bb01b26..176b947 100644 --- a/db/query/branch.sql +++ b/db/query/branch.sql @@ -61,7 +61,8 @@ SET name = COALESCE(sqlc.narg(name), name), location = COALESCE(sqlc.narg(location), location), branch_manager_id = COALESCE(sqlc.narg(branch_manager_id), branch_manager_id), company_id = COALESCE(sqlc.narg(company_id), company_id), - is_self_owned = COALESCE(sqlc.narg(is_self_owned), is_self_owned) + is_self_owned = COALESCE(sqlc.narg(is_self_owned), is_self_owned), + is_active = COALESCE(sqlc.narg(is_active), is_active) WHERE id = $1 RETURNING *; -- name: DeleteBranch :exec diff --git a/gen/db/branch.sql.go b/gen/db/branch.sql.go index 92e7f80..ad59526 100644 --- a/gen/db/branch.sql.go +++ b/gen/db/branch.sql.go @@ -443,7 +443,8 @@ SET name = COALESCE($2, name), location = COALESCE($3, location), branch_manager_id = COALESCE($4, branch_manager_id), company_id = COALESCE($5, company_id), - is_self_owned = COALESCE($6, is_self_owned) + is_self_owned = COALESCE($6, is_self_owned), + is_active = COALESCE($7, is_active) WHERE id = $1 RETURNING id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at ` @@ -455,6 +456,7 @@ type UpdateBranchParams struct { BranchManagerID pgtype.Int8 `json:"branch_manager_id"` CompanyID pgtype.Int8 `json:"company_id"` IsSelfOwned pgtype.Bool `json:"is_self_owned"` + IsActive pgtype.Bool `json:"is_active"` } func (q *Queries) UpdateBranch(ctx context.Context, arg UpdateBranchParams) (Branch, error) { @@ -465,6 +467,7 @@ func (q *Queries) UpdateBranch(ctx context.Context, arg UpdateBranchParams) (Bra arg.BranchManagerID, arg.CompanyID, arg.IsSelfOwned, + arg.IsActive, ) var i Branch err := row.Scan( diff --git a/internal/domain/branch.go b/internal/domain/branch.go index 43d2cc0..99876ed 100644 --- a/internal/domain/branch.go +++ b/internal/domain/branch.go @@ -53,6 +53,7 @@ type UpdateBranch struct { BranchManagerID *int64 CompanyID *int64 IsSelfOwned *bool + IsActive *bool } type CreateSupportedOperation struct { diff --git a/internal/repository/branch.go b/internal/repository/branch.go index 51f460f..a9f9980 100644 --- a/internal/repository/branch.go +++ b/internal/repository/branch.go @@ -83,6 +83,12 @@ func convertUpdateBranch(updateBranch domain.UpdateBranch) dbgen.UpdateBranchPar Valid: true, } } + if updateBranch.IsActive != nil { + newUpdateBranch.IsActive = pgtype.Bool{ + Bool: *updateBranch.IsActive, + Valid: true, + } + } return newUpdateBranch } diff --git a/internal/web_server/handlers/branch_handler.go b/internal/web_server/handlers/branch_handler.go index 6f869a1..f5d5866 100644 --- a/internal/web_server/handlers/branch_handler.go +++ b/internal/web_server/handlers/branch_handler.go @@ -2,6 +2,7 @@ package handlers import ( "strconv" + "strings" "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/authentication" @@ -682,6 +683,42 @@ func (h *Handler) UpdateBranch(c *fiber.Ctx) error { } +func (h *Handler) UpdateBranchStatus(c *fiber.Ctx) error { + branchID := c.Params("id") + id, err := strconv.ParseInt(branchID, 10, 64) + if err != nil { + h.logger.Error("Invalid branch ID", "branchID", branchID, "error", err) + return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid branch ID", err, nil) + } + + var isActive bool + path := strings.Split(strings.Trim(c.Path(), "/"), "/") + + if path[len(path)-1] == "set-active" { + isActive = true + } else if path[len(path)-1] == "set-inactive" { + isActive = false + } else { + h.logger.Error("Invalid branch status", "status", isActive, "error", err) + return response.WriteJSON(c, fiber.StatusBadRequest, "Invalid branch status", err, nil) + } + + branch, err := h.branchSvc.UpdateBranch(c.Context(), domain.UpdateBranch{ + ID: id, + IsActive: &isActive, + }) + + if err != nil { + h.logger.Error("Failed to update branch", "branchID", id, "error", err) + return response.WriteJSON(c, fiber.StatusInternalServerError, "Failed to update branch", err, nil) + } + + res := convertBranch(branch) + + return response.WriteJSON(c, fiber.StatusOK, "Branch Updated", res, nil) + +} + // DeleteBranch godoc // @Summary Delete the branch // @Description Delete the branch diff --git a/internal/web_server/routes.go b/internal/web_server/routes.go index 784338a..363b577 100644 --- a/internal/web_server/routes.go +++ b/internal/web_server/routes.go @@ -146,6 +146,8 @@ func (a *App) initAppRoutes() { a.fiber.Get("/branch/:id", a.authMiddleware, h.GetBranchByID) a.fiber.Get("/branch/:id/bets", a.authMiddleware, h.GetBetByBranchID) a.fiber.Put("/branch/:id", a.authMiddleware, h.UpdateBranch) + a.fiber.Put("/branch/:id/set-active", a.authMiddleware, h.UpdateBranchStatus) + a.fiber.Put("/branch/:id/set-inactive", a.authMiddleware, h.UpdateBranchStatus) a.fiber.Delete("/branch/:id", a.authMiddleware, h.DeleteBranch) a.fiber.Get("/search/branch", a.authMiddleware, h.SearchBranch) // /branch/search diff --git a/logs/app.log b/logs/app.log index e69de29..d95137b 100644 --- a/logs/app.log +++ b/logs/app.log @@ -0,0 +1,6 @@ +time=2025-06-15T11:01:26.408+03:00 level=INFO msg="No events were updated" service_info.env=development +time=2025-06-15T11:01:26.410+03:00 level=INFO msg="Successfully processed results" service_info.env=development removed_events=0 total_events=0 +time=2025-06-15T11:01:26.411+03:00 level=INFO msg="Starting server" service_info.env=development port=8080 +time=2025-06-15T11:07:09.604+03:00 level=INFO msg="No events were updated" service_info.env=development +time=2025-06-15T11:07:09.605+03:00 level=INFO msg="Successfully processed results" service_info.env=development removed_events=0 total_events=0 +time=2025-06-15T11:07:09.606+03:00 level=INFO msg="Starting server" service_info.env=development port=8080