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

dalai2547/sucu-52-documents-update-doc-by-id #11

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions cmd/server/fiber_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,7 @@ func (s *FiberHttpServer) initDocumentRouter(router fiber.Router, httpHandler ha
documentRouter := router.Group("/documents")

documentRouter.Post("/", httpHandler.Middleware().IsLogin, httpHandler.Document().CreateDocument)
documentRouter.Put("/:document_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.Document().UpdateDocumentByID)
documentRouter.Delete("/:document_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.Document().DeleteDocumentByID)

}
22 changes: 22 additions & 0 deletions internal/domain/usecases/document_usecase.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usecases

import (
"errors"
"fmt"

"github.com/isd-sgcu/sucu-backend-2024/internal/domain/entities"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/isd-sgcu/sucu-backend-2024/utils"
"github.com/isd-sgcu/sucu-backend-2024/utils/constant"
"go.uber.org/zap"
"gorm.io/gorm"
)

type documentUsecase struct {
Expand Down Expand Up @@ -75,9 +77,29 @@ func (u *documentUsecase) CreateDocument(document *dtos.CreateDocumentDTO) *appe
}

func (u *documentUsecase) UpdateDocumentByID(ID string, updateMap interface{}) *apperror.AppError {
if err := u.documentRepository.UpdateDocumentByID(ID, updateMap); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
u.logger.Named("UpdateDocumentByID").Error(constant.ErrDocumentNotFound, zap.String("documentID", ID))
return apperror.InternalServerError(constant.ErrDocumentNotFound)
}
u.logger.Named("UpdateDocumentByID").Error(constant.ErrUpdateDocumentFailed, zap.String("documentID", ID), zap.Error(err))
return apperror.InternalServerError(constant.ErrUpdateDocumentFailed)
}

u.logger.Named("UpdateDocumentByID").Info("Success: Document updated", zap.String("documentID", ID))
return nil
}

func (u *documentUsecase) DeleteDocumentByID(ID string) *apperror.AppError {
if err := u.documentRepository.DeleteDocumentByID(ID); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
u.logger.Named("DeleteDocumentByID").Error(constant.ErrDocumentNotFound, zap.String("documentID", ID))
return apperror.InternalServerError(constant.ErrDocumentNotFound)
}
u.logger.Named("DeleteDocumentByID").Error(constant.ErrDeleteDocumentFailed, zap.String("documentID", ID), zap.Error(err))
return apperror.InternalServerError(constant.ErrDeleteDocumentFailed)
}

u.logger.Named("DeleteDocumentByID").Info("Success: Document deleted", zap.String("documentID", ID))
return nil
}
38 changes: 36 additions & 2 deletions internal/interface/handlers/document_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,28 @@ func (h *DocumentHandler) CreateDocument(c *fiber.Ctx) error {
// @Failure 500 {object} response.Response
// @Router /documents/{document_id} [put]
func (h *DocumentHandler) UpdateDocumentByID(c *fiber.Ctx) error {
return nil
documentID := c.Params("document_id")
if documentID == "" {
resp := response.NewResponseFactory(response.ERROR, "Document ID is required")
return resp.SendResponse(c, fiber.StatusBadRequest)
}

// Parse the request body into UpdateDocumentDTO
var updateDocumentDTO dtos.UpdateDocumentDTO
if err := c.BodyParser(&updateDocumentDTO); err != nil {
resp := response.NewResponseFactory(response.ERROR, "Invalid request body")
return resp.SendResponse(c, fiber.StatusBadRequest)
}

apperr := h.documentUsecase.UpdateDocumentByID(documentID, updateDocumentDTO)
if apperr != nil {
resp := response.NewResponseFactory(response.ERROR, apperr.Error())
return resp.SendResponse(c, apperr.HttpCode)
}

// Return success response
resp := response.NewResponseFactory(response.SUCCESS, "Document updated successfully")
return resp.SendResponse(c, fiber.StatusOK)
}

// DeleteDocumentByID godoc
Expand All @@ -120,5 +141,18 @@ func (h *DocumentHandler) UpdateDocumentByID(c *fiber.Ctx) error {
// @Failure 500 {object} response.Response
// @Router /documents/{document_id} [delete]
func (h *DocumentHandler) DeleteDocumentByID(c *fiber.Ctx) error {
return nil
documentID := c.Params("document_id")
if documentID == "" {
resp := response.NewResponseFactory(response.ERROR, "Document ID is required")
return resp.SendResponse(c, fiber.StatusBadRequest)
}

apperr := h.documentUsecase.DeleteDocumentByID(documentID)
if apperr != nil {
resp := response.NewResponseFactory(response.ERROR, apperr.Error())
return resp.SendResponse(c, apperr.HttpCode)
}

resp := response.NewResponseFactory(response.SUCCESS, "Document deleted successfully")
return resp.SendResponse(c, fiber.StatusOK)
}
2 changes: 1 addition & 1 deletion internal/interface/repositories/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ type DocumentRepository interface {
FindDocumentsByRole(roles *[]string) (*[]entities.Document, error)
InsertDocument(document *entities.Document) error
UpdateDocumentByID(ID string, updateMap interface{}) error
DeleteUserByID(ID string) error
DeleteDocumentByID(ID string) error
}
6 changes: 3 additions & 3 deletions internal/interface/repositories/document_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func (r *documentRepository) InsertDocument(document *entities.Document) error {
}

func (r *documentRepository) UpdateDocumentByID(ID string, updateMap interface{}) error {
return nil
return r.db.Model(&entities.Document{}).Where("id = ?", ID).Updates(updateMap).Error
}

func (r *documentRepository) DeleteUserByID(ID string) error {
return nil
func (r *documentRepository) DeleteDocumentByID(ID string) error {
return r.db.Where("id = ?", ID).Delete(&entities.Document{}).Error
}
4 changes: 4 additions & 0 deletions utils/constant/error_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ var (
// doc error
ErrInvalidDocType = "invalid document type"
ErrInsertDocumentFailed = "failed to insert document"
ErrDocumentNotFound = "document not found"
ErrFindDocumentByID = "failed to find document by ID"
ErrUpdateDocumentFailed = "failed to update document"
ErrDeleteDocumentFailed = "failed to delete document"
)