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 63 file get all files typedocs #19

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions cmd/server/fiber_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ func (s *FiberHttpServer) initUserRouter(router fiber.Router, httpHandler handle
func (s *FiberHttpServer) initAttachmentRouter(router fiber.Router, httpHandler handlers.Handler) {
attachmentRouter := router.Group("/attachments")

attachmentRouter.Get("/", httpHandler.Attachment().GetAllAttachments)
attachmentRouter.Get("/role/:role_id", httpHandler.Attachment().GetAllAttachmentsByRole)
attachmentRouter.Post("/:document_id", httpHandler.Attachment().CreateAttachments)
attachmentRouter.Delete("/:attachment_id", httpHandler.Middleware().IsLogin, httpHandler.Middleware().SuperAdmin, httpHandler.Attachment().DeleteAttachment)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/domain/usecases/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

type AttachmentUsecase interface {
// client side
GetAllAttachments() (*[]dtos.AttachmentDTO, *apperror.AppError)
GetAllAttachments(req *dtos.GetAllAttachmentsDTO) (*dtos.PaginationResponse, *apperror.AppError)

// back office
GetAllAttachmentsByRole(req dtos.UserDTO) (*[]dtos.AttachmentDTO, *apperror.AppError)
GetAllAttachmentsByRole(req *dtos.GetAllAttachmentsByRoleDTO) (*dtos.PaginationResponse, *apperror.AppError)
CreateAttachments(documentID string, files map[string][]*multipart.FileHeader) *apperror.AppError
DeleteAttachment(ID string) *apperror.AppError
}
82 changes: 78 additions & 4 deletions internal/domain/usecases/attachment_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"math"
"mime/multipart"
"strings"

Expand Down Expand Up @@ -32,12 +33,85 @@ func NewAttachmentUsecase(cfg config.Config, logger *zap.Logger, attachmentRepos
}
}

func (u *attachmentUsecase) GetAllAttachments() (*[]dtos.AttachmentDTO, *apperror.AppError) {
return nil, nil
func (u *attachmentUsecase) GetAllAttachments(req *dtos.GetAllAttachmentsDTO) (*dtos.PaginationResponse, *apperror.AppError) {

args := &repositories.FindAllAttachmentsArgs{
Offset: (req.Page - 1) * req.PageSize,
Limit: req.PageSize,
AttachmentType: req.AttachmentType,
DisplayName: req.DisplayName,
StartTime: req.StartTime,
EndTime: req.EndTime,
}

attachments, err := u.attachmentRepository.FindAllAttachments(args)
if err != nil {
u.logger.Named("GetAllAttachments").Error(constant.ErrGetAttachmentFailed, zap.Error(err))
return nil, apperror.InternalServerError(constant.ErrGetAttachmentFailed)
}

// create pagination response dtos
data := make([]map[string]interface{}, 0)
for _, d := range *attachments {
data = append(data, map[string]interface{}{
"id": d.ID,
"display_name": d.DisplayName,
"document_id": d.Document.ID,
"type": strings.ToLower(d.TypeID),
"created_at": d.CreatedAt,
"updated_at": d.UpdatedAt,
})
}

paginationResponse := dtos.PaginationResponse{
Data: data,
Page: fmt.Sprintf("%d", req.Page),
Limit: fmt.Sprintf("%d", req.PageSize),
TotalPage: fmt.Sprintf("%d", (int(math.Ceil(float64(len(data)) / float64(req.PageSize))))),
}

return &paginationResponse, nil

}

func (u *attachmentUsecase) GetAllAttachmentsByRole(req dtos.UserDTO) (*[]dtos.AttachmentDTO, *apperror.AppError) {
return nil, nil
func (u *attachmentUsecase) GetAllAttachmentsByRole(req *dtos.GetAllAttachmentsByRoleDTO) (*dtos.PaginationResponse, *apperror.AppError) {
args := &repositories.FindAllAttachmentsByRoleArgs{
Offset: (req.Page - 1) * req.PageSize,
Limit: req.PageSize,
AttachmentType: req.AttachmentType,
DisplayName: req.DisplayName,
StartTime: req.StartTime,
EndTime: req.EndTime,
Role: req.Role,
}

attachments, err := u.attachmentRepository.FindAllAttachmentsByRole(args)
if err != nil {
u.logger.Named("GetAllAttachmentsByRole").Error(constant.ErrGetAttachmentFailed, zap.Error(err))
return nil, apperror.InternalServerError(constant.ErrGetAttachmentFailed)
}

// create pagination response dtos
data := make([]map[string]interface{}, 0)
for _, d := range *attachments {
data = append(data, map[string]interface{}{
"id": d.ID,
"display_name": d.DisplayName,
"document_id": d.Document.ID,
"type": strings.ToLower(d.TypeID),
"created_at": d.CreatedAt,
"updated_at": d.UpdatedAt,
})
}

paginationResponse := dtos.PaginationResponse{
Data: data,
Page: fmt.Sprintf("%d", req.Page),
Limit: fmt.Sprintf("%d", req.PageSize),
TotalPage: fmt.Sprintf("%d", (int(math.Ceil(float64(len(data)) / float64(req.PageSize))))),
}

return &paginationResponse, nil
}

func (u *attachmentUsecase) CreateAttachments(documentID string, files map[string][]*multipart.FileHeader) *apperror.AppError {
Expand Down
19 changes: 19 additions & 0 deletions internal/interface/dtos/attachment_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,22 @@ type AttachmentDTO struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

type GetAllAttachmentsDTO struct {
Page int
PageSize int
DisplayName string
AttachmentType string // type: docs, image
StartTime time.Time
EndTime time.Time
}

type GetAllAttachmentsByRoleDTO struct {
Page int
PageSize int
DisplayName string
AttachmentType string // type: docs, image
Role string
StartTime time.Time
EndTime time.Time
}
89 changes: 87 additions & 2 deletions internal/interface/handlers/attachment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package handlers

import (
"strings"
"time"

"github.com/gofiber/fiber/v2"
"github.com/isd-sgcu/sucu-backend-2024/internal/domain/usecases"
"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/utils"
"github.com/isd-sgcu/sucu-backend-2024/utils/constant"
)

type AttachmentHandler struct {
Expand All @@ -26,7 +30,45 @@ func NewAttachmentHandler(attachmentUsecase usecases.AttachmentUsecase) *Attachm
// @Failure 400 {object} response.Response
// @Router /attachments [get]
func (h *AttachmentHandler) GetAllAttachments(c *fiber.Ctx) error {
return nil
getallAttachmentsDTO := dtos.GetAllAttachmentsDTO{
Page: c.QueryInt("page", 1),
PageSize: c.QueryInt("page_size", 10),
DisplayName: c.Query("name"),
AttachmentType: c.Query("attachment_type"),
}

var errors []string

if !utils.ValidateDocType(getallAttachmentsDTO.AttachmentType) {
errors = append(errors, constant.ErrInvalidAttachmentType)
}

if ps := getallAttachmentsDTO.PageSize; ps > constant.MAX_PAGE_SIZE || ps < 0 {
errors = append(errors, constant.ErrInvalidPageSize)
}

startTime, err1 := time.Parse(time.RFC3339, c.Query("start_time", time.Time{}.UTC().Format(time.RFC3339)))
endTime, err2 := time.Parse(time.RFC3339, c.Query("end_time", time.Now().UTC().Format(time.RFC3339)))
if err1 != nil || err2 != nil {
errors = append(errors, constant.ErrInvalidTimeFormat)
}

if len(errors) != 0 {
resp := response.NewResponseFactory(response.ERROR, strings.Join(errors, ", "))
return resp.SendResponse(c, fiber.StatusBadRequest)
}

getallAttachmentsDTO.StartTime = startTime
getallAttachmentsDTO.EndTime = endTime

paginationResp, err := h.attachmentUsecase.GetAllAttachments(&getallAttachmentsDTO)
if err != nil {
resp := response.NewResponseFactory(response.ERROR, err.Error())
return resp.SendResponse(c, err.HttpCode)
}

resp := response.NewResponseFactory(response.SUCCESS, paginationResp)
return resp.SendResponse(c, fiber.StatusOK)
}

// GetAllAttachmentsByRole godoc
Expand All @@ -38,7 +80,50 @@ func (h *AttachmentHandler) GetAllAttachments(c *fiber.Ctx) error {
// @Failure 400 {object} response.Response
// @Router /attachments/role/{role_id} [get]
func (h *AttachmentHandler) GetAllAttachmentsByRole(c *fiber.Ctx) error {
return nil
getallAttachmentsByRole := dtos.GetAllAttachmentsByRoleDTO{
Page: c.QueryInt("page", 1),
PageSize: c.QueryInt("page_size", 10),
DisplayName: c.Query("name"),
AttachmentType: c.Query("attachment_type"),
Role: c.Params("role_id"),
}

var errors []string

if !utils.ValidateAttachmentType(getallAttachmentsByRole.AttachmentType) {
errors = append(errors, constant.ErrInvalidAttachmentType)
}

if ps := getallAttachmentsByRole.PageSize; ps > constant.MAX_PAGE_SIZE || ps < 0 {
errors = append(errors, constant.ErrInvalidPageSize)
}

if role := getallAttachmentsByRole.Role; role == "" || !utils.ValidateRole(role) {
errors = append(errors, constant.ErrInvalidRole)
}

startTime, err1 := time.Parse(time.RFC3339, c.Query("start_time", time.Time{}.UTC().Format(time.RFC3339)))
endTime, err2 := time.Parse(time.RFC3339, c.Query("end_time", time.Now().UTC().Format(time.RFC3339)))
if err1 != nil || err2 != nil {
errors = append(errors, constant.ErrInvalidTimeFormat)
}

if len(errors) != 0 {
resp := response.NewResponseFactory(response.ERROR, strings.Join(errors, ", "))
return resp.SendResponse(c, fiber.StatusBadRequest)
}

getallAttachmentsByRole.StartTime = startTime
getallAttachmentsByRole.EndTime = endTime

paginationResp, err := h.attachmentUsecase.GetAllAttachmentsByRole(&getallAttachmentsByRole)
if err != nil {
resp := response.NewResponseFactory(response.ERROR, err.Error())
return resp.SendResponse(c, err.HttpCode)
}

resp := response.NewResponseFactory(response.SUCCESS, paginationResp)
return resp.SendResponse(c, fiber.StatusOK)
}

// CreateAttachments godoc
Expand Down
5 changes: 2 additions & 3 deletions internal/interface/repositories/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (

type AttachmentRepository interface {
// client side
FindAllAttachments() (*entities.Attachment, error)
FindAllAttachments(args *FindAllAttachmentsArgs) (*[]entities.Attachment, error)

// back office
FindAttachmentByID(ID string) (*entities.Attachment, error)

FindAllAttachmentsByRole(args *FindAllAttachmentsByRoleArgs) (*[]entities.Attachment, error)
InsertAttachments(attachments *[]entities.Attachment) error
UploadAttachmentToS3(bucketName string, fileReaders map[string]io.Reader) error

Expand Down
Loading