feat: implement system statistics tracking, commit management controllers, and associated database migrations
All checks were successful
Build and Release / release (push) Successful in 1m49s

This commit is contained in:
2026-05-07 11:31:53 +07:00
parent ca05785a24
commit bdaac7ddd8
29 changed files with 1347 additions and 2 deletions

View File

@@ -136,3 +136,35 @@ func (h *CommitController) GetProjectCommits(c fiber.Ctx) error {
Data: res,
})
}
// GetCommitByID godoc
// @Summary Get commit by ID
// @Description Retrieve a specific commit by its ID
// @Tags Commits
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param commitId path string true "Commit ID"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 404 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/commits/{commitId} [get]
func (h *CommitController) GetCommitByID(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
commitID := c.Params("commitId")
res, err := h.service.GetCommitByID(ctx, commitID)
if err != nil {
return c.Status(err.Code).JSON(response.CommonResponse{
Status: false,
Message: err.Message,
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}

View File

@@ -0,0 +1,96 @@
package controllers
import (
"history-api/internal/dtos/request"
"history-api/internal/dtos/response"
"history-api/internal/services"
"history-api/pkg/validator"
"github.com/gofiber/fiber/v3"
)
type StatisticController struct {
statService services.StatisticService
}
func NewStatisticController(statService services.StatisticService) *StatisticController {
return &StatisticController{
statService: statService,
}
}
// SearchStatistics godoc
// @Summary Search system statistics
// @Description Fetch daily system statistics with optional date range filtering
// @Tags Statistics
// @Accept json
// @Produce json
// @Param start_date query string false "Start date in YYYY-MM-DD format"
// @Param end_date query string false "End date in YYYY-MM-DD format"
// @Success 200 {object} response.CommonResponse{data=[]response.StatisticResponse}
// @Failure 400 {object} response.CommonResponse
// @Failure 401 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /statistics [get]
// @Security BearerAuth
func (c *StatisticController) SearchStatistics(ctx fiber.Ctx) error {
dto := new(request.SearchStatisticDto)
if err := validator.ValidateQueryDto(ctx, dto); err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
res, err := c.statService.Search(ctx.Context(), dto)
if err != nil {
return ctx.Status(err.Code).JSON(response.CommonResponse{
Status: false,
Message: err.Message,
})
}
return ctx.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// GetStatisticByDate godoc
// @Summary Get system statistics by date
// @Description Fetch system statistics for a specific date
// @Tags Statistics
// @Accept json
// @Produce json
// @Param date path string true "Date in YYYY-MM-DD format"
// @Success 200 {object} response.CommonResponse{data=response.StatisticResponse}
// @Failure 400 {object} response.CommonResponse
// @Failure 401 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 404 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /statistics/{date} [get]
// @Security BearerAuth
func (c *StatisticController) GetStatisticByDate(ctx fiber.Ctx) error {
dateStr := ctx.Params("date")
if dateStr == "" {
return ctx.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Message: "Date parameter is required",
})
}
res, err := c.statService.GetByDate(ctx.Context(), dateStr)
if err != nil {
return ctx.Status(err.Code).JSON(response.CommonResponse{
Status: false,
Message: err.Message,
})
}
return ctx.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}

View File

@@ -526,3 +526,80 @@ func (h *UserController) GetProjectByUserID(c fiber.Ctx) error {
Data: res,
})
}
// AdminUpdateProfile godoc
// @Summary Update user profile (Admin/Mod only)
// @Description Update the profile details of any user
// @Tags Users
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "User ID"
// @Param request body request.UpdateProfileDto true "Update Profile request"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /users/{id} [put]
func (h *UserController) AdminUpdateProfile(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
userId := c.Params("id")
dto := &request.UpdateProfileDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
res, err := h.service.UpdateProfile(ctx, userId, dto)
if err != nil {
return c.Status(err.Code).JSON(response.CommonResponse{
Status: false,
Message: err.Message,
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// AdminResetPassword godoc
// @Summary Reset user password (Admin/Mod only)
// @Description Reset the password for any user without requiring the old password
// @Tags Users
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "User ID"
// @Param request body request.ResetPasswordDto true "Reset Password request"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /users/{id}/password [patch]
func (h *UserController) AdminResetPassword(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
userId := c.Params("id")
dto := &request.ResetPasswordDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
err := h.service.AdminResetPassword(ctx, userId, dto)
if err != nil {
return c.Status(err.Code).JSON(response.CommonResponse{
Status: false,
Message: err.Message,
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Message: "Password reset successfully",
})
}