Module project, commit, submission
All checks were successful
Build and Release / release (push) Successful in 1m15s

This commit is contained in:
2026-04-26 16:31:03 +07:00
parent ac90236022
commit 6918a100fc
60 changed files with 5957 additions and 1020 deletions

View File

@@ -0,0 +1,137 @@
package controllers
import (
"context"
"history-api/internal/dtos/request"
"history-api/internal/dtos/response"
"history-api/internal/services"
"history-api/pkg/validator"
"time"
"github.com/gofiber/fiber/v3"
)
type CommitController struct {
service services.CommitService
}
func NewCommitController(service services.CommitService) *CommitController {
return &CommitController{
service: service,
}
}
// CreateCommit godoc
// @Summary Create a new commit
// @Description Create a new commit and update project's latest commit ID. Only owner/editor allowed.
// @Tags Commits
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param request body request.CreateCommitDto true "Commit Data"
// @Success 201 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/commits [post]
func (h *CommitController) CreateCommit(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
dto := &request.CreateCommitDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
uid := c.Locals("uid").(string)
res, err := h.service.CreateCommit(ctx, uid, projectID, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusCreated).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// RestoreCommit godoc
// @Summary Restore project to a commit
// @Description Update project's latest commit ID to an older commit. Only owner/editor allowed.
// @Tags Commits
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param request body request.RestoreCommitDto true "Restore Data"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 404 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/commits/restore [post]
func (h *CommitController) RestoreCommit(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
dto := &request.RestoreCommitDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
uid := c.Locals("uid").(string)
err := h.service.RestoreCommit(ctx, uid, projectID, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Message: "Project restored successfully",
})
}
// GetProjectCommits godoc
// @Summary Get project commits
// @Description Retrieve all commits for a specific project
// @Tags Commits
// @Accept json
// @Produce json
// @Param id path string true "Project ID"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/commits [get]
func (h *CommitController) GetProjectCommits(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
res, err := h.service.GetProjectCommits(ctx, projectID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}

View File

@@ -198,3 +198,170 @@ func (h *ProjectController) DeleteProject(c fiber.Ctx) error {
Message: "Project deleted successfully",
})
}
// AddMember godoc
// @Summary Add a member to project
// @Description Invite a user to the project with a specific role (EDITOR or VIEWER). Only project owner can do this.
// @Tags Projects
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param request body request.AddProjectMemberDto true "Member Data"
// @Success 201 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 409 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/members [post]
func (h *ProjectController) AddMember(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
dto := &request.AddProjectMemberDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
uid := c.Locals("uid").(string)
res, err := h.service.AddMember(ctx, uid, projectID, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusCreated).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// UpdateMemberRole godoc
// @Summary Update member role
// @Description Change a member's role in the project. Only project owner can do this.
// @Tags Projects
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param userId path string true "Member User ID"
// @Param request body request.UpdateProjectMemberDto true "Role Data"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 404 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/members/{userId} [put]
func (h *ProjectController) UpdateMemberRole(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
memberUserID := c.Params("userId")
dto := &request.UpdateProjectMemberDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
uid := c.Locals("uid").(string)
res, err := h.service.UpdateMemberRole(ctx, uid, projectID, memberUserID, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// RemoveMember godoc
// @Summary Remove a member from project
// @Description Remove a user from the project. Only project owner can do this.
// @Tags Projects
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param userId path string true "Member User ID"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 404 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/members/{userId} [delete]
func (h *ProjectController) RemoveMember(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
memberUserID := c.Params("userId")
uid := c.Locals("uid").(string)
err := h.service.RemoveMember(ctx, uid, projectID, memberUserID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Message: "Member removed successfully",
})
}
// ChangeOwner godoc
// @Summary Transfer project ownership
// @Description Transfer project ownership to an existing member. Only the current owner can do this.
// @Tags Projects
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path string true "Project ID"
// @Param request body request.ChangeOwnerDto true "New Owner Data"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 403 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Router /projects/{id}/change-owner [put]
func (h *ProjectController) ChangeOwner(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
projectID := c.Params("id")
dto := &request.ChangeOwnerDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
uid := c.Locals("uid").(string)
res, err := h.service.ChangeOwner(ctx, uid, projectID, dto.NewOwnerID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}

View File

@@ -0,0 +1,213 @@
package controllers
import (
"context"
"history-api/internal/dtos/request"
"history-api/internal/dtos/response"
"history-api/internal/services"
"history-api/pkg/validator"
"time"
"github.com/gofiber/fiber/v3"
)
type SubmissionController interface {
CreateSubmission(c fiber.Ctx) error
UpdateSubmissionStatus(c fiber.Ctx) error
GetSubmissionByID(c fiber.Ctx) error
SearchSubmissions(c fiber.Ctx) error
DeleteSubmission(c fiber.Ctx) error
}
type submissionController struct {
submissionService services.SubmissionService
}
func NewSubmissionController(submissionService services.SubmissionService) SubmissionController {
return &submissionController{
submissionService: submissionService,
}
}
// @Summary Create submission
// @Description Submit a new submission for a project commit
// @Tags Submission
// @Accept json
// @Produce json
// @Param body body request.CreateSubmissionDto true "Submission data"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Security BearerAuth
// @Router /submissions [post]
func (s *submissionController) CreateSubmission(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dto := &request.CreateSubmissionDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
res, err := s.submissionService.CreateSubmission(ctx, c.Locals("uid").(string), dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// @Summary Update submission status
// @Description Approve or reject a submission
// @Tags Submission
// @Accept json
// @Produce json
// @Param id path string true "Submission ID"
// @Param body body request.UpdateSubmissionStatusDto true "Status update data"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Security BearerAuth
// @Router /submissions/{id}/status [patch]
func (s *submissionController) UpdateSubmissionStatus(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
id := c.Params("id")
uid := c.Locals("uid").(string)
dto := &request.UpdateSubmissionStatusDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
res, err := s.submissionService.UpdateSubmissionStatus(ctx, uid, id, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// @Summary Get submission by ID
// @Description Get detailed information of a submission
// @Tags Submission
// @Produce json
// @Param id path string true "Submission ID"
// @Success 200 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Security BearerAuth
// @Router /submissions/{id} [get]
func (s *submissionController) GetSubmissionByID(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
id := c.Params("id")
res, err := s.submissionService.GetSubmissionByID(ctx, id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// @Summary Search submissions
// @Description Get a list of submissions with filters
// @Tags Submission
// @Accept json
// @Produce json
// @Param query query request.SearchSubmissionDto false "Search Query"
// @Success 200 {object} response.CommonResponse
// @Failure 400 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Security BearerAuth
// @Router /submissions [get]
func (s *submissionController) SearchSubmissions(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
dto := &request.SearchSubmissionDto{}
if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Errors: err,
})
}
res, err := s.submissionService.SearchSubmissions(ctx, dto)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// @Summary Delete submission
// @Description Delete a submission by ID
// @Tags Submission
// @Produce json
// @Param id path string true "Submission ID"
// @Success 200 {object} response.CommonResponse
// @Failure 401 {object} response.CommonResponse
// @Failure 500 {object} response.CommonResponse
// @Security BearerAuth
// @Router /submissions/{id} [delete]
func (s *submissionController) DeleteSubmission(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
id := c.Params("id")
claimsVal := c.Locals("user_claims")
if claimsVal == nil {
return c.Status(fiber.StatusUnauthorized).JSON(response.CommonResponse{
Status: false,
Message: "Unauthorized",
})
}
claims, ok := claimsVal.(*response.JWTClaims)
if !ok {
return c.Status(fiber.StatusUnauthorized).JSON(response.CommonResponse{
Status: false,
Message: "Invalid user claims",
})
}
err := s.submissionService.DeleteSubmission(ctx, c.Locals("uid").(string), id, claims)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Message: "Submission deleted successfully",
})
}

View File

@@ -46,7 +46,7 @@ func (h *UserController) GetUserCurrent(c fiber.Ctx) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := h.service.GetUserCurrent(ctx, c.Locals("uid").(string))
res, err := h.service.GetUserByID(ctx, c.Locals("uid").(string))
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
Status: false,

View File

@@ -2,8 +2,8 @@ package controllers
import (
"context"
"history-api/internal/dtos/response"
"history-api/internal/dtos/request"
"history-api/internal/dtos/response"
"history-api/internal/services"
"history-api/pkg/validator"
"time"
@@ -63,7 +63,7 @@ func (m *VerificationController) SearchVerification(c fiber.Ctx) error {
dto := &request.SearchUserVerificationDto{}
if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Status: false,
Errors: err,
})
}
@@ -137,7 +137,7 @@ func (m *VerificationController) CreateVerification(c fiber.Ctx) error {
dto := &request.CreateUserVerificationDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Status: false,
Errors: err,
})
}
@@ -148,7 +148,10 @@ func (m *VerificationController) CreateVerification(c fiber.Ctx) error {
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(res)
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}
// @Summary Update application status
@@ -170,7 +173,7 @@ func (m *VerificationController) UpdateVerificationStatus(c fiber.Ctx) error {
dto := &request.UpdateVerificationStatusDto{}
if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false,
Status: false,
Errors: err,
})
}
@@ -182,5 +185,8 @@ func (m *VerificationController) UpdateVerificationStatus(c fiber.Ctx) error {
Message: err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(res)
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
Status: true,
Data: res,
})
}