-
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
base: dev
Are you sure you want to change the base?
Conversation
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure about this, but I think u.documentRepository.UpdateDocumentByID
will return gorm.ErrRecordNotFound
if there's no documents with that id. So, you don't have to u.documentRepository.FindDocumentByID
and maybe move if errors.Is(err, gorm.ErrRecordNotFound)
into if err := u.documentRepository.UpdateDocumentByID(ID, updateMap)
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.
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 comment
The 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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
thanks that looks good to me
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 comment
The reason will be displayed to describe this comment to others. Learn more.
also here
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 comment
The 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)
}
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 comment
The reason will be displayed to describe this comment to others. Learn more.
also here
cmd/server/fiber_server.go
Outdated
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) |
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
cmd/server/fiber_server.go
Outdated
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 comment
The 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
cmd/server/fiber_server.go
Outdated
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) |
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
cmd/server/fiber_server.go
Outdated
} | ||
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
please update with latest dev branch kub
cmd/server/fiber_server.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
apply middleware here kub, both using .IsLogin() middleware kub
@bookpanda @wiraphatys |
No description provided.