-
Notifications
You must be signed in to change notification settings - Fork 0
/
artist.go
48 lines (37 loc) · 1.04 KB
/
artist.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
package adapter
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/scarlet0725/prism-api/model"
"github.com/scarlet0725/prism-api/usecase"
)
type ArtistAdapter interface {
CreateArtist(*gin.Context)
}
type artistAdapter struct {
artist usecase.Artist
}
func NewArtistAdapter(artist usecase.Artist) ArtistAdapter {
return &artistAdapter{artist: artist}
}
func (a *artistAdapter) CreateArtist(ctx *gin.Context) {
var req model.CreateArtist
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"ok": false, "msg": "Bad Request"})
return
}
if req.Name == "" {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"ok": false, "msg": "Artist name is required"})
return
}
artist := &model.Artist{
Name: req.Name,
URL: req.URL,
}
result, err := a.artist.CreateArtist(artist)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"ok": false, "msg": "Failed to create artist"})
return
}
ctx.JSON(http.StatusOK, gin.H{"ok": true, "artist": result})
}