Chore: project init
This commit is contained in:
36
internal/download/handler.go
Normal file
36
internal/download/handler.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package download
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
type Handler struct {
|
||||
svc *Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *Service) *Handler {
|
||||
return &Handler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *Handler) GetInfo(c *fiber.Ctx) error {
|
||||
info, err := h.svc.GetInfo()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "다운로드 정보가 없습니다"})
|
||||
}
|
||||
return c.JSON(info)
|
||||
}
|
||||
|
||||
func (h *Handler) Upsert(c *fiber.Ctx) error {
|
||||
var body struct {
|
||||
URL string `json:"url"`
|
||||
Version string `json:"version"`
|
||||
FileName string `json:"fileName"`
|
||||
FileSize string `json:"fileSize"`
|
||||
}
|
||||
if err := c.BodyParser(&body); err != nil || body.URL == "" || body.Version == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "url과 version은 필수입니다"})
|
||||
}
|
||||
info, err := h.svc.Upsert(body.URL, body.Version, body.FileName, body.FileSize)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "업데이트에 실패했습니다"})
|
||||
}
|
||||
return c.JSON(info)
|
||||
}
|
||||
11
internal/download/model.go
Normal file
11
internal/download/model.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package download
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Info struct {
|
||||
gorm.Model
|
||||
URL string `gorm:"not null"`
|
||||
Version string `gorm:"not null"`
|
||||
FileName string `gorm:"not null"`
|
||||
FileSize string `gorm:"not null"`
|
||||
}
|
||||
24
internal/download/repository.go
Normal file
24
internal/download/repository.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package download
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) GetLatest() (*Info, error) {
|
||||
var info Info
|
||||
err := r.db.Last(&info).Error
|
||||
return &info, err
|
||||
}
|
||||
|
||||
func (r *Repository) Save(info *Info) error {
|
||||
if info.ID == 0 {
|
||||
return r.db.Create(info).Error
|
||||
}
|
||||
return r.db.Save(info).Error
|
||||
}
|
||||
25
internal/download/service.go
Normal file
25
internal/download/service.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package download
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) GetInfo() (*Info, error) {
|
||||
return s.repo.GetLatest()
|
||||
}
|
||||
|
||||
func (s *Service) Upsert(url, version, fileName, fileSize string) (*Info, error) {
|
||||
info, err := s.repo.GetLatest()
|
||||
if err != nil {
|
||||
info = &Info{}
|
||||
}
|
||||
info.URL = url
|
||||
info.Version = version
|
||||
info.FileName = fileName
|
||||
info.FileSize = fileSize
|
||||
return info, s.repo.Save(info)
|
||||
}
|
||||
Reference in New Issue
Block a user