-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,19 +119,19 @@ func (s *FiberHttpServer) initUserRouter(router fiber.Router, httpHandler handle | |
userRouter := router.Group("/users") | ||
|
||
userRouter.Get("/", httpHandler.User().GetAllUsers) | ||
userRouter.Post("/", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.User().CreateUser) | ||
userRouter.Patch("/", httpHandler.Middleware().IsLogin, httpHandler.User().UpdateProfile) | ||
userRouter.Put("/:user_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.User().UpdateUserByID) | ||
userRouter.Post("/", httpHandler.User().CreateUser) | ||
} | ||
|
||
func (s *FiberHttpServer) initAttachmentRouter(router fiber.Router, httpHandler handlers.Handler) { | ||
attachmentRouter := router.Group("/attachments") | ||
|
||
attachmentRouter.Post("/:document_id", httpHandler.Attachment().CreateAttachments) | ||
attachmentRouter.Get("/:document_id", httpHandler.Attachment().CreateAttachments) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please update with latest dev branch kub |
||
} | ||
|
||
func (s *FiberHttpServer) initDocumentRouter(router fiber.Router, httpHandler handlers.Handler) { | ||
documentRouter := router.Group("/documents") | ||
documentsRouter := router.Group("/documents") | ||
|
||
documentsRouter.Post("/", httpHandler.Document().CreateDocument) | ||
documentsRouter.Delete("/:document_id", httpHandler.Document().DeleteDocumentByID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For post/delete routes, let's add some middlewares to e.g. documentsRouter.Post("/", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.Document().CreateDocument)
documentsRouter.Delete("/:document_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.Document().DeleteDocumentByID) If you have any problems trying to test routes that requires login, we can talk na There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apply middleware here kub, both using .IsLogin() middleware kub |
||
|
||
documentRouter.Post("/", httpHandler.Middleware().IsLogin, httpHandler.Document().CreateDocument) | ||
} |
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" | ||
|
@@ -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 { | ||
|
@@ -75,9 +77,41 @@ func (u *documentUsecase) CreateDocument(document *dtos.CreateDocumentDTO) *appe | |
} | ||
|
||
func (u *documentUsecase) UpdateDocumentByID(ID string, updateMap interface{}) *apperror.AppError { | ||
existingDocument, err := u.documentRepository.FindDocumentByID(ID) | ||
if 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.ErrFindDocumentByID, zap.String("documentID", ID), zap.Error(err)) | ||
return apperror.InternalServerError(constant.ErrFindDocumentByID) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure about this, but I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure too bro @bookpanda, please check it kub @D33102 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it will return ErrRecordNotFound since when I tested with non-recorded ID, it didn't give any error. @bookpanda @wiraphatys There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks that looks good to me |
||
|
||
if err := u.documentRepository.UpdateDocumentByID(ID, updateMap); err != nil { | ||
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", existingDocument.ID)) | ||
return nil | ||
} | ||
|
||
func (u *documentUsecase) DeleteDocumentByID(ID string) *apperror.AppError { | ||
existingDocument, err := u.documentRepository.FindDocumentByID(ID) | ||
|
||
if 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.ErrFindDocumentByID, zap.String("documentID", ID), zap.Error(err)) | ||
return apperror.InternalServerError(constant.ErrFindDocumentByID) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
if err := u.documentRepository.DeleteDocumentByID(ID); err != nil { | ||
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", existingDocument.ID)) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"github.com/isd-sgcu/sucu-backend-2024/internal/interface/dtos" | ||
"github.com/isd-sgcu/sucu-backend-2024/pkg/response" | ||
"github.com/isd-sgcu/sucu-backend-2024/pkg/validator" | ||
"github.com/isd-sgcu/sucu-backend-2024/utils/constant" | ||
) | ||
|
||
type DocumentHandler struct { | ||
|
@@ -107,7 +108,32 @@ 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) | ||
} | ||
|
||
err := h.documentUsecase.UpdateDocumentByID(documentID, updateDocumentDTO) | ||
if err != nil { | ||
if err.Error() == "document not found" { | ||
resp := response.NewResponseFactory(response.ERROR, "Document not found") | ||
return resp.SendResponse(c, fiber.StatusNotFound) | ||
} | ||
resp := response.NewResponseFactory(response.ERROR, "Failed to update document") | ||
return resp.SendResponse(c, fiber.StatusInternalServerError) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use this instead so that the logic for checking status code is all in usecase layer 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 | ||
|
@@ -120,5 +146,22 @@ 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) | ||
} | ||
|
||
err := h.documentUsecase.DeleteDocumentByID(documentID) | ||
if err != nil { | ||
if err.Error() == constant.ErrDocumentNotFound { | ||
resp := response.NewResponseFactory(response.ERROR, constant.ErrDocumentNotFound) | ||
return resp.SendResponse(c, fiber.StatusNotFound) | ||
} | ||
resp := response.NewResponseFactory(response.ERROR, constant.ErrDeleteDocumentFailed) | ||
return resp.SendResponse(c, fiber.StatusInternalServerError) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here |
||
|
||
resp := response.NewResponseFactory(response.SUCCESS, "Document deleted successfully") | ||
return resp.SendResponse(c, fiber.StatusOK) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you updated with dev branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update with latest dev branch kub