2024-03-05 19:15:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2024-05-05 09:24:49 +00:00
|
|
|
"context"
|
2025-01-20 04:43:40 +00:00
|
|
|
"errors"
|
2024-05-05 09:24:49 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
2024-03-05 19:15:53 +00:00
|
|
|
"github.com/rs/zerolog"
|
2024-05-05 09:24:49 +00:00
|
|
|
"go-humas-be/app/database/entity"
|
2024-11-04 01:12:22 +00:00
|
|
|
articleCategoriesRepository "go-humas-be/app/module/article_categories/repository"
|
2025-01-20 04:43:40 +00:00
|
|
|
articleCategoryDetailsRepository "go-humas-be/app/module/article_category_details/repository"
|
|
|
|
|
articleCategoryDetailsReq "go-humas-be/app/module/article_category_details/request"
|
2024-05-07 08:17:26 +00:00
|
|
|
articleFilesRepository "go-humas-be/app/module/article_files/repository"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/app/module/articles/mapper"
|
|
|
|
|
"go-humas-be/app/module/articles/repository"
|
|
|
|
|
"go-humas-be/app/module/articles/request"
|
|
|
|
|
"go-humas-be/app/module/articles/response"
|
2024-05-05 09:24:49 +00:00
|
|
|
usersRepository "go-humas-be/app/module/users/repository"
|
2025-01-20 04:43:40 +00:00
|
|
|
config "go-humas-be/config/config"
|
2024-05-05 09:24:49 +00:00
|
|
|
minioStorage "go-humas-be/config/config"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/utils/paginator"
|
2024-05-05 09:24:49 +00:00
|
|
|
utilSvc "go-humas-be/utils/service"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"mime"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2024-03-05 19:15:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ArticlesService
|
|
|
|
|
type articlesService struct {
|
2025-01-20 04:43:40 +00:00
|
|
|
Repo repository.ArticlesRepository
|
|
|
|
|
ArticleCategoriesRepo articleCategoriesRepository.ArticleCategoriesRepository
|
|
|
|
|
ArticleFilesRepo articleFilesRepository.ArticleFilesRepository
|
|
|
|
|
ArticleCategoryDetailsRepo articleCategoryDetailsRepository.ArticleCategoryDetailsRepository
|
|
|
|
|
Log zerolog.Logger
|
|
|
|
|
Cfg *config.Config
|
|
|
|
|
UsersRepo usersRepository.UsersRepository
|
|
|
|
|
MinioStorage *minioStorage.MinioStorage
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArticlesService define interface of IArticlesService
|
|
|
|
|
type ArticlesService interface {
|
|
|
|
|
All(req request.ArticlesQueryRequest) (articles []*response.ArticlesResponse, paging paginator.Pagination, err error)
|
|
|
|
|
Show(id uint) (articles *response.ArticlesResponse, err error)
|
2024-05-05 09:24:49 +00:00
|
|
|
Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error)
|
|
|
|
|
SaveThumbnail(c *fiber.Ctx) (err error)
|
2024-03-05 19:15:53 +00:00
|
|
|
Update(id uint, req request.ArticlesUpdateRequest) (err error)
|
|
|
|
|
Delete(id uint) error
|
2024-05-05 09:24:49 +00:00
|
|
|
Viewer(c *fiber.Ctx) error
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewArticlesService init ArticlesService
|
2024-05-05 09:24:49 +00:00
|
|
|
func NewArticlesService(
|
|
|
|
|
repo repository.ArticlesRepository,
|
2024-11-04 01:12:22 +00:00
|
|
|
articleCategoriesRepo articleCategoriesRepository.ArticleCategoriesRepository,
|
2025-01-20 04:43:40 +00:00
|
|
|
articleCategoryDetailsRepo articleCategoryDetailsRepository.ArticleCategoryDetailsRepository,
|
2024-05-07 08:17:26 +00:00
|
|
|
articleFilesRepo articleFilesRepository.ArticleFilesRepository,
|
2024-05-05 09:24:49 +00:00
|
|
|
log zerolog.Logger,
|
2025-01-20 04:43:40 +00:00
|
|
|
cfg *config.Config,
|
2024-05-05 09:24:49 +00:00
|
|
|
usersRepo usersRepository.UsersRepository,
|
|
|
|
|
minioStorage *minioStorage.MinioStorage,
|
|
|
|
|
) ArticlesService {
|
2024-03-05 19:15:53 +00:00
|
|
|
|
|
|
|
|
return &articlesService{
|
2025-01-20 04:43:40 +00:00
|
|
|
Repo: repo,
|
|
|
|
|
ArticleCategoriesRepo: articleCategoriesRepo,
|
|
|
|
|
ArticleCategoryDetailsRepo: articleCategoryDetailsRepo,
|
|
|
|
|
ArticleFilesRepo: articleFilesRepo,
|
|
|
|
|
Log: log,
|
|
|
|
|
UsersRepo: usersRepo,
|
|
|
|
|
MinioStorage: minioStorage,
|
|
|
|
|
Cfg: cfg,
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All implement interface of ArticlesService
|
|
|
|
|
func (_i *articlesService) All(req request.ArticlesQueryRequest) (articless []*response.ArticlesResponse, paging paginator.Pagination, err error) {
|
|
|
|
|
results, paging, err := _i.Repo.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:articlesService", "Methods:All").
|
|
|
|
|
Interface("results", results).Msg("")
|
|
|
|
|
|
2025-01-20 04:43:40 +00:00
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
port := _i.Cfg.App.ExternalPort
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
for _, result := range results {
|
2025-01-20 04:43:40 +00:00
|
|
|
articleRes := mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleFilesRepo, _i.UsersRepo)
|
2024-05-07 11:08:38 +00:00
|
|
|
articless = append(articless, articleRes)
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesService) Show(id uint) (articles *response.ArticlesResponse, err error) {
|
|
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-20 04:43:40 +00:00
|
|
|
host := _i.Cfg.App.Domain
|
|
|
|
|
port := _i.Cfg.App.ExternalPort
|
|
|
|
|
|
|
|
|
|
return mapper.ArticlesResponseMapper(_i.Log, host+port, result, _i.ArticleCategoriesRepo, _i.ArticleFilesRepo, _i.UsersRepo), nil
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
func (_i *articlesService) Save(req request.ArticlesCreateRequest, authToken string) (articles *entity.Articles, err error) {
|
2024-03-05 19:15:53 +00:00
|
|
|
_i.Log.Info().Interface("data", req).Msg("")
|
2024-05-05 09:24:49 +00:00
|
|
|
newReq := req.ToEntity()
|
|
|
|
|
|
|
|
|
|
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
|
|
|
|
|
newReq.CreatedById = &createdBy.ID
|
|
|
|
|
|
2025-01-20 04:43:40 +00:00
|
|
|
saveArticleRepo, err := _i.Repo.Create(newReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var categoryIds []string
|
|
|
|
|
if req.CategoryIds != "" {
|
|
|
|
|
categoryIds = strings.Split(req.CategoryIds, ",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for categoryId := range categoryIds {
|
|
|
|
|
findCategory, err := _i.ArticleCategoriesRepo.FindOne(uint(categoryId))
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if findCategory != nil {
|
|
|
|
|
return nil, errors.New("category not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
categoryReq := articleCategoryDetailsReq.ArticleCategoryDetailsCreateRequest{
|
|
|
|
|
ArticleId: saveArticleRepo.ID,
|
|
|
|
|
CategoryId: categoryId,
|
|
|
|
|
IsActive: true,
|
|
|
|
|
}
|
|
|
|
|
newCategoryReq := categoryReq.ToEntity()
|
|
|
|
|
|
|
|
|
|
err = _i.ArticleCategoryDetailsRepo.Create(newCategoryReq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return saveArticleRepo, nil
|
2024-05-05 09:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesService) SaveThumbnail(c *fiber.Ctx) (err error) {
|
|
|
|
|
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:articlesService", "Methods:SaveThumbnail").
|
|
|
|
|
Interface("id", id).Msg("")
|
|
|
|
|
|
|
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
|
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
files := form.File["files"]
|
|
|
|
|
|
|
|
|
|
// Create minio connection.
|
|
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Return status 500 and minio connection error.
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
|
|
|
"error": true,
|
|
|
|
|
"msg": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Iterasi semua file yang diunggah
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "Uploader:: loop1").
|
|
|
|
|
Interface("data", file).Msg("")
|
|
|
|
|
|
|
|
|
|
src, err := file.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer src.Close()
|
2024-03-05 19:15:53 +00:00
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
filename := filepath.Base(file.Filename)
|
2024-05-07 17:56:49 +00:00
|
|
|
filename = strings.ReplaceAll(filename, " ", "")
|
2024-05-05 09:24:49 +00:00
|
|
|
filenameWithoutExt := filepath.Clean(filename[:len(filename)-len(filepath.Ext(filename))])
|
|
|
|
|
extension := filepath.Ext(file.Filename)[1:]
|
|
|
|
|
|
|
|
|
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
randUniqueId := rand.Intn(1000000)
|
|
|
|
|
|
|
|
|
|
newFilenameWithoutExt := filenameWithoutExt + "_" + strconv.Itoa(randUniqueId)
|
|
|
|
|
newFilename := newFilenameWithoutExt + "." + extension
|
|
|
|
|
objectName := "articles/thumbnail/" + newFilename
|
|
|
|
|
|
|
|
|
|
findCategory, err := _i.Repo.FindOne(uint(id))
|
|
|
|
|
findCategory.ThumbnailName = &newFilename
|
|
|
|
|
findCategory.ThumbnailPath = &objectName
|
|
|
|
|
err = _i.Repo.Update(uint(id), findCategory)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload file ke MinIO
|
|
|
|
|
_, err = minioClient.PutObject(context.Background(), bucketName, objectName, src, file.Size, minio.PutObjectOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesService) Update(id uint, req request.ArticlesUpdateRequest) (err error) {
|
|
|
|
|
_i.Log.Info().Interface("data", req).Msg("")
|
|
|
|
|
return _i.Repo.Update(id, req.ToEntity())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesService) Delete(id uint) error {
|
2024-03-31 15:19:45 +00:00
|
|
|
result, err := _i.Repo.FindOne(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isActive := false
|
|
|
|
|
result.IsActive = &isActive
|
|
|
|
|
return _i.Repo.Update(id, result)
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
2024-05-05 09:24:49 +00:00
|
|
|
|
|
|
|
|
func (_i *articlesService) Viewer(c *fiber.Ctx) (err error) {
|
|
|
|
|
thumbnailName := c.Params("thumbnailName")
|
|
|
|
|
|
2024-11-06 18:45:46 +00:00
|
|
|
emptyImage := "empty-image.jpg"
|
|
|
|
|
searchThumbnail := emptyImage
|
|
|
|
|
if thumbnailName != emptyImage {
|
|
|
|
|
result, err := _i.Repo.FindByFilename(thumbnailName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "articlesService:Viewer").
|
|
|
|
|
Interface("resultThumbnail", result.ThumbnailPath).Msg("")
|
|
|
|
|
|
|
|
|
|
if result.ThumbnailPath != nil {
|
|
|
|
|
searchThumbnail = *result.ThumbnailPath
|
|
|
|
|
} else {
|
|
|
|
|
searchThumbnail = "articles/thumbnail/" + emptyImage
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
searchThumbnail = "articles/thumbnail/" + emptyImage
|
2024-05-05 09:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-06 18:45:46 +00:00
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
|
|
|
Format(time.RFC3339)).Str("Service:Resource", "articlesService:Viewer").
|
|
|
|
|
Interface("searchThumbnail", searchThumbnail).Msg("")
|
|
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
bucketName := _i.MinioStorage.Cfg.ObjectStorage.MinioStorage.BucketName
|
2024-11-06 18:45:46 +00:00
|
|
|
objectName := searchThumbnail
|
2024-05-05 09:24:49 +00:00
|
|
|
|
|
|
|
|
// Create minio connection.
|
|
|
|
|
minioClient, err := _i.MinioStorage.ConnectMinio()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Return status 500 and minio connection error.
|
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
|
|
|
"error": true,
|
|
|
|
|
"msg": err.Error(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 18:45:46 +00:00
|
|
|
fileContent, err := minioClient.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
|
2024-05-05 09:24:49 +00:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalln(err)
|
|
|
|
|
}
|
|
|
|
|
defer fileContent.Close()
|
|
|
|
|
|
2024-11-06 18:45:46 +00:00
|
|
|
contentType := mime.TypeByExtension("." + getFileExtension(objectName))
|
2024-05-05 09:24:49 +00:00
|
|
|
if contentType == "" {
|
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Set("Content-Type", contentType)
|
|
|
|
|
|
|
|
|
|
if _, err := io.Copy(c.Response().BodyWriter(), fileContent); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getFileExtension(filename string) string {
|
|
|
|
|
// split file name
|
|
|
|
|
parts := strings.Split(filename, ".")
|
|
|
|
|
|
|
|
|
|
// jika tidak ada ekstensi, kembalikan string kosong
|
|
|
|
|
if len(parts) == 1 || (len(parts) == 2 && parts[0] == "") {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ambil ekstensi terakhir
|
|
|
|
|
return parts[len(parts)-1]
|
|
|
|
|
}
|