Skip to content

Commit

Permalink
refactor: add dto
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Apr 25, 2022
1 parent 5f3727a commit b8cad11
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
32 changes: 32 additions & 0 deletions core/dto/preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dto

import (
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
)

type Preset struct {
TitleNew string `json:"title_new"`
URL string `json:"url"`
URLNew string `json:"url_new"`
}

func ConvertPreset(p *Preset) (*preset.Preset, error) {
return preset.ParsePreset(p.URL, p.TitleNew, p.URLNew)
}

func NewPreset(p *preset.Preset) Preset {
return Preset{
TitleNew: p.TitleNew,
URL: p.URL,
URLNew: p.URLNew,
}
}

func NewPresets(ps []preset.Preset) []Preset {
presets := make([]Preset, len(ps))
for i, p := range ps {
presets[i] = Preset{TitleNew: p.TitleNew, URL: p.URL, URLNew: p.URLNew}
}

return presets
}
6 changes: 3 additions & 3 deletions core/preset/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

type (
Preset struct {
TitleNew string `json:"title_new"`
URL string `json:"url"`
URLNew string `json:"url_new"`
TitleNew string
URL string
URLNew string
}

PresetStore interface {
Expand Down
27 changes: 13 additions & 14 deletions left/api/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/ItsNotGoodName/reciva-web-remote/core"
"github.com/ItsNotGoodName/reciva-web-remote/core/dto"
"github.com/ItsNotGoodName/reciva-web-remote/core/preset"
"github.com/ItsNotGoodName/reciva-web-remote/left/presenter"
)
Expand All @@ -25,57 +26,54 @@ func GetPreset(presetStore preset.PresetStore) presenter.Requester {
return func(r *http.Request) presenter.Response {
url := r.URL.Query().Get("url")

// Get preset
p, err := presetStore.Get(r.Context(), url)
if err != nil {
return handlePresetError(err)
}

return presenter.Response{
Code: http.StatusOK,
Data: p,
Data: dto.NewPreset(p),
}
}
}

func GetPresets(presetStore preset.PresetStore) presenter.Requester {
return func(r *http.Request) presenter.Response {
ps, err := presetStore.List(r.Context())
// List presets
p, err := presetStore.List(r.Context())
if err != nil {
handlePresetError(err)
}

return presenter.Response{
Code: http.StatusOK,
Data: ps,
Data: dto.NewPresets(p),
}
}
}

func PostPreset(presetStore preset.PresetStore) presenter.Requester {
type request struct {
URL string `json:"url"`
TitleNew string `json:"title_new"`
URLNew string `json:"url_new"`
}

return func(r *http.Request) presenter.Response {
req := request{}
err := json.NewDecoder(r.Body).Decode(&req)
dtoPreset := &dto.Preset{}
err := json.NewDecoder(r.Body).Decode(dtoPreset)
if err != nil {
return presenter.Response{
Code: http.StatusBadRequest,
Error: err,
}
}

p, err := preset.ParsePreset(req.URL, req.TitleNew, req.URLNew)
p, err := dto.ConvertPreset(dtoPreset)
if err != nil {
return presenter.Response{
Code: http.StatusBadRequest,
Error: err,
}
}

// Update preset
if err := presetStore.Update(r.Context(), p); err != nil {
return handlePresetError(err)
}
Expand All @@ -88,12 +86,13 @@ func PostPreset(presetStore preset.PresetStore) presenter.Requester {

func GetPresetURL(presetStore preset.PresetStore, url string) http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
preset, err := presetStore.Get(r.Context(), url)
// Get preset
dtoPreset, err := presetStore.Get(r.Context(), url)
if err != nil {
http.Error(rw, err.Error(), handlePresetError(err).Code)
return
}

rw.Write([]byte(preset.URLNew))
rw.Write([]byte(dto.NewPreset(dtoPreset).URLNew))
}
}
2 changes: 1 addition & 1 deletion left/api/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func RequireRadio(hub radio.HubService, next RadioRequester) presenter.Requester
return func(r *http.Request) presenter.Response {
uuid := chi.URLParam(r, "uuid")

// Get radio
rd, err := hub.Get(uuid)
if err != nil {
return handleRadioError(err)
Expand Down Expand Up @@ -111,7 +112,6 @@ func PatchRadio(radioService radio.RadioService) RadioRequester {
}

return func(r *http.Request, rd radio.Radio) presenter.Response {
// Parse body
var radioPatch RadioPatch
if err := json.NewDecoder(r.Body).Decode(&radioPatch); err != nil {
return presenter.Response{
Expand Down

0 comments on commit b8cad11

Please sign in to comment.