-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanga.go
119 lines (92 loc) · 2.79 KB
/
manga.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// service.go
package service
import (
"trabalho/db/models"
"trabalho/repository"
"trabalho/utils"
"mime/multipart"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type MangaService struct {
Repository repository.MangaRepository
}
func (s *MangaService) CreateManga(ctx *gin.Context, body models.Manga, coverImage *multipart.FileHeader) (models.Manga, error) {
mangaID := utils.GenerateUUID()
coverImagePath, err := utils.SaveCoverImage(ctx, coverImage, mangaID)
if err != nil {
return models.Manga{}, err
}
defer utils.RemoveFileServerSide(coverImagePath)
manga := createManga(body, coverImagePath, mangaID)
if err := s.Repository.SaveManga(&manga); err != nil {
return models.Manga{}, err
}
if err := utils.UploadCoverImageToS3(coverImagePath); err != nil {
return models.Manga{}, err
}
return manga, nil
}
func (s *MangaService) UpdateManga(id uuid.UUID, updatedManga models.Manga) (models.Manga, error) {
manga, err := s.Repository.GetMangaByID(id)
if err != nil {
return models.Manga{}, err
}
manga.Author = updatedManga.Author
manga.Title = updatedManga.Title
manga.Description = updatedManga.Description
manga.Synopsis = updatedManga.Synopsis
manga.Tags = updatedManga.Tags
if err := s.Repository.SaveManga(&manga); err != nil {
return models.Manga{}, err
}
return manga, nil
}
func (s *MangaService) UpdateMangaImage(ctx *gin.Context, id uuid.UUID, coverImage *multipart.FileHeader) (models.Manga, error) {
manga, err := s.Repository.GetMangaByID(id)
if err != nil {
return models.Manga{}, err
}
oldCoverImagePath := manga.ImageCover
newCoverImagePath, err := utils.SaveCoverImage(ctx, coverImage, id)
if err != nil {
return models.Manga{}, err
}
defer utils.RemoveFileServerSide(newCoverImagePath)
manga.ImageCover = newCoverImagePath
if err := s.Repository.UpdateManga(&manga); err != nil {
return models.Manga{}, err
}
if err := utils.DeleteCoverImageFromS3(oldCoverImagePath); err != nil {
return models.Manga{},err
}
return manga, nil
}
func (s *MangaService) GetMangaByID(id uuid.UUID) (models.Manga, error) {
return s.Repository.GetMangaByID(id)
}
func (s *MangaService) DeleteManga(id uuid.UUID) error {
manga, err := s.Repository.GetMangaByID(id)
if err != nil {
return err
}
return s.Repository.DeleteManga(&manga)
}
func (s *MangaService) ListMangas(offset, limit int) ([]models.Manga, error) {
mangas, err := s.Repository.ListMangas(offset, limit)
if err != nil {
return nil, err
}
return mangas, nil
}
func createManga(body models.Manga, coverImagePath string, mangaID uuid.UUID) models.Manga {
return models.Manga{
ID: mangaID,
Author: body.Author,
Title: body.Title,
Description: body.Description,
Synopsis: body.Synopsis,
Tags: body.Tags,
ImageCover: coverImagePath,
}
}