129 lines
3.0 KiB
Go
129 lines
3.0 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"github.com/gofiber/fiber/v2"
|
||
|
|
|
||
|
|
"web-qudo-be/app/module/hero_section/repository"
|
||
|
|
hero_section_image_service "web-qudo-be/app/module/hero_section_image/service"
|
||
|
|
)
|
||
|
|
|
||
|
|
type HeroSectionService struct {
|
||
|
|
Repo *repository.HeroSectionRepository
|
||
|
|
ImageService *hero_section_image_service.HeroSectionImageService
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewHeroSectionService(
|
||
|
|
repo *repository.HeroSectionRepository,
|
||
|
|
imageService *hero_section_image_service.HeroSectionImageService,
|
||
|
|
) *HeroSectionService {
|
||
|
|
return &HeroSectionService{
|
||
|
|
Repo: repo,
|
||
|
|
ImageService: imageService,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// GET
|
||
|
|
func (s *HeroSectionService) Get(ctx context.Context) (interface{}, error) {
|
||
|
|
data, err := s.Repo.Get(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// ambil image (optional)
|
||
|
|
if id, ok := data["id"].(int); ok {
|
||
|
|
image, _ := s.ImageService.GetByHeroID(ctx, id)
|
||
|
|
data["image"] = image
|
||
|
|
}
|
||
|
|
|
||
|
|
return data, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// CREATE
|
||
|
|
func (s *HeroSectionService) Create(ctx context.Context, c *fiber.Ctx) error {
|
||
|
|
type request struct {
|
||
|
|
PrimaryTitle string `json:"primary_title"`
|
||
|
|
SecondaryTitle string `json:"secondary_title"`
|
||
|
|
Description string `json:"description"`
|
||
|
|
PrimaryCTA string `json:"primary_cta"`
|
||
|
|
SecondaryCTAText string `json:"secondary_cta_text"`
|
||
|
|
}
|
||
|
|
|
||
|
|
req := new(request)
|
||
|
|
|
||
|
|
if err := c.BodyParser(req); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// insert hero_content
|
||
|
|
heroID, err := s.Repo.Create(ctx, map[string]interface{}{
|
||
|
|
"primary_title": req.PrimaryTitle,
|
||
|
|
"secondary_title": req.SecondaryTitle,
|
||
|
|
"description": req.Description,
|
||
|
|
"primary_cta": req.PrimaryCTA,
|
||
|
|
"secondary_cta_text": req.SecondaryCTAText,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// OPTIONAL: upload image kalau ada
|
||
|
|
file, err := c.FormFile("image")
|
||
|
|
if err == nil {
|
||
|
|
path := "./uploads/" + file.Filename
|
||
|
|
c.SaveFile(file, path)
|
||
|
|
|
||
|
|
url := "/uploads/" + file.Filename
|
||
|
|
|
||
|
|
_ = s.ImageService.Upload(ctx, heroID, path, url)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// UPDATE
|
||
|
|
func (s *HeroSectionService) Update(ctx context.Context, id int, c *fiber.Ctx) error {
|
||
|
|
type request struct {
|
||
|
|
PrimaryTitle string `json:"primary_title"`
|
||
|
|
SecondaryTitle string `json:"secondary_title"`
|
||
|
|
Description string `json:"description"`
|
||
|
|
PrimaryCTA string `json:"primary_cta"`
|
||
|
|
SecondaryCTAText string `json:"secondary_cta_text"`
|
||
|
|
}
|
||
|
|
|
||
|
|
req := new(request)
|
||
|
|
|
||
|
|
if err := c.BodyParser(req); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
err := s.Repo.Update(ctx, id, map[string]interface{}{
|
||
|
|
"primary_title": req.PrimaryTitle,
|
||
|
|
"secondary_title": req.SecondaryTitle,
|
||
|
|
"description": req.Description,
|
||
|
|
"primary_cta": req.PrimaryCTA,
|
||
|
|
"secondary_cta_text": req.SecondaryCTAText,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
// update image kalau ada upload baru
|
||
|
|
file, err := c.FormFile("image")
|
||
|
|
if err == nil {
|
||
|
|
path := "./uploads/" + file.Filename
|
||
|
|
c.SaveFile(file, path)
|
||
|
|
|
||
|
|
url := "/uploads/" + file.Filename
|
||
|
|
|
||
|
|
_ = s.ImageService.Upload(ctx, id, path, url)
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// DELETE
|
||
|
|
func (s *HeroSectionService) Delete(ctx context.Context, id int) error {
|
||
|
|
return s.Repo.Delete(ctx, id)
|
||
|
|
}
|