-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from isd-sgcu/gear/joh-54-johnjud-gateway-imag…
…e-endopint Gear/joh 54 johnjud gateway image endopint
- Loading branch information
Showing
14 changed files
with
407 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,26 @@ | ||
package dto | ||
|
||
type ImageDto struct { | ||
type ImageResponse struct { | ||
Id string `json:"id"` | ||
Url string `json:"url"` | ||
ObjectKey string `json:"object_key"` | ||
} | ||
|
||
type UploadImageRequest struct { | ||
Filename string `json:"filename" validate:"required"` | ||
Data []byte `json:"data" validate:"required"` | ||
PetId string `json:"pet_id" validate:"required"` | ||
} | ||
|
||
type DeleteImageResponse struct { | ||
Success bool `json:"success"` | ||
} | ||
|
||
type AssignPetRequest struct { | ||
Ids []string `json:"ids" validate:"required"` | ||
PetId string `json:"pet_id" validate:"required"` | ||
} | ||
|
||
type AssignPetResponse struct { | ||
Success bool `json:"success"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,133 @@ | ||
package auth | ||
package image | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/isd-sgcu/johnjud-gateway/src/app/constant" | ||
"github.com/isd-sgcu/johnjud-gateway/src/app/dto" | ||
"github.com/isd-sgcu/johnjud-gateway/src/app/router" | ||
"github.com/isd-sgcu/johnjud-gateway/src/app/validator" | ||
imageSvc "github.com/isd-sgcu/johnjud-gateway/src/pkg/service/image" | ||
) | ||
|
||
type Handler struct { | ||
service imageSvc.Service | ||
validate *validator.DtoValidator | ||
validate validator.IDtoValidator | ||
} | ||
|
||
func NewHandler(service imageSvc.Service, validate *validator.DtoValidator) *Handler { | ||
func NewHandler(service imageSvc.Service, validate validator.IDtoValidator) *Handler { | ||
return &Handler{service, validate} | ||
} | ||
|
||
func (h *Handler) FindByPetId(c *router.FiberCtx) { | ||
id, err := c.ID() | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusInternalServerError, | ||
Message: constant.InvalidIDMessage, | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
response, respErr := h.service.FindByPetId(id) | ||
if respErr != nil { | ||
c.JSON(respErr.StatusCode, respErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, response) | ||
return | ||
} | ||
|
||
func (h *Handler) Upload(c *router.FiberCtx) { | ||
request := &dto.UploadImageRequest{} | ||
err := c.Bind(request) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusBadRequest, | ||
Message: constant.BindingRequestErrorMessage + err.Error(), | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
if err := h.validate.Validate(request); err != nil { | ||
var errorMessage []string | ||
for _, reqErr := range err { | ||
errorMessage = append(errorMessage, reqErr.Message) | ||
} | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusBadRequest, | ||
Message: constant.InvalidRequestBodyMessage + strings.Join(errorMessage, ", "), | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
response, respErr := h.service.Upload(request) | ||
if respErr != nil { | ||
c.JSON(respErr.StatusCode, respErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, response) | ||
return | ||
} | ||
|
||
func (h *Handler) Delete(c *router.FiberCtx) { | ||
id, err := c.ID() | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusBadRequest, | ||
Message: err.Error(), | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
res, errRes := h.service.Delete(id) | ||
if errRes != nil { | ||
c.JSON(errRes.StatusCode, errRes) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, res.Success) | ||
return | ||
} | ||
|
||
func (h *Handler) AssignPet(c *router.FiberCtx) { | ||
request := &dto.AssignPetRequest{} | ||
err := c.Bind(request) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusBadRequest, | ||
Message: constant.BindingRequestErrorMessage + err.Error(), | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
if err := h.validate.Validate(request); err != nil { | ||
var errorMessage []string | ||
for _, reqErr := range err { | ||
errorMessage = append(errorMessage, reqErr.Message) | ||
} | ||
c.JSON(http.StatusBadRequest, dto.ResponseErr{ | ||
StatusCode: http.StatusBadRequest, | ||
Message: constant.InvalidRequestBodyMessage + strings.Join(errorMessage, ", "), | ||
Data: nil, | ||
}) | ||
return | ||
} | ||
|
||
response, respErr := h.service.AssignPet(request) | ||
if respErr != nil { | ||
c.JSON(respErr.StatusCode, respErr) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, response) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.