-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ad3400
commit 2435c64
Showing
12 changed files
with
360 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,55 @@ | ||
package mapper | ||
|
||
import "github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain/responses" | ||
import ( | ||
"fmt" | ||
|
||
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain" | ||
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain/responses" | ||
) | ||
|
||
func MakeResponsePostMedia(id int) *responses.PostMedia { | ||
return &responses.PostMedia{ | ||
ID: id, | ||
} | ||
} | ||
|
||
func MakeResponseGetDefaultMedia(defaultMedia *domain.DefaultMedia, media *domain.MediaFile) (*responses.GetDefaultMedia, error) { | ||
if defaultMedia == nil || media == nil { | ||
return nil, fmt.Errorf("got nil defaultMedia or media") | ||
} | ||
if defaultMedia.MediaID != media.ID { | ||
return nil, fmt.Errorf("got not matching media_id in defaultMedia and media id: %v != %v", defaultMedia.MediaID, media.ID) | ||
} | ||
|
||
resp := responses.GetDefaultMedia{} | ||
resp.DefaultID = defaultMedia.ID | ||
resp.ID = media.ID | ||
resp.Key = media.Key | ||
resp.Name = media.Name | ||
|
||
return &resp, nil | ||
} | ||
|
||
func MakeResponseAllDefaultMedia(defaultMedias []domain.DefaultMedia, mediaFiles map[int]domain.MediaFile) (*responses.GetAllDefaultMedia, error) { | ||
resp := responses.GetAllDefaultMedia{} | ||
for _, defaultMedia := range defaultMedias { | ||
media, ok := mediaFiles[defaultMedia.MediaID] | ||
if !ok { | ||
return nil, fmt.Errorf("can't find media for default media id %v", defaultMedia.MediaID) | ||
} | ||
dMedia := responses.GetDefaultMedia{} | ||
dMedia.DefaultID = defaultMedia.ID | ||
dMedia.ID = media.ID | ||
dMedia.Key = media.Key | ||
dMedia.Name = media.Name | ||
resp.Media = append(resp.Media, dMedia) | ||
} | ||
return &resp, nil | ||
} | ||
|
||
func MakeResponsePostDefaultMedia(id, mediaID int) *responses.PostDefaultMedia { | ||
return &responses.PostDefaultMedia{ | ||
ID: id, | ||
MediaId: mediaID, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package requests | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/go-chi/chi" | ||
) | ||
|
||
type GetDefaultMedia struct { | ||
ID int `json:"id"` | ||
} | ||
|
||
func (c *GetDefaultMedia) Bind(req *http.Request) error { | ||
id, err := strconv.Atoi(chi.URLParam(req, "id")) | ||
if err != nil { | ||
return fmt.Errorf("can't Atoi on id in request: %w", err) | ||
} | ||
|
||
c.ID = id | ||
return c.validate() | ||
} | ||
|
||
func (c *GetDefaultMedia) validate() error { | ||
if c.ID == 0 { | ||
return fmt.Errorf("require: id") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package requests | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type PostDefaultMedia struct { | ||
PostMedia | ||
} | ||
|
||
type PostMediaDefaultPointer struct { | ||
PostMediaPointer | ||
} | ||
|
||
func (m *PostDefaultMedia) Bind(req *http.Request) error { | ||
decoder := json.NewDecoder(req.Body) | ||
decoder.DisallowUnknownFields() | ||
pm := PostMediaDefaultPointer{} | ||
err := decoder.Decode(&pm) | ||
if err != nil { | ||
return err | ||
} | ||
if decoder.More() { | ||
return err | ||
} | ||
err = pm.validate() | ||
if err != nil { | ||
return err | ||
} | ||
*m = PostDefaultMedia{ | ||
PostMedia: PostMedia{ | ||
Name: *pm.Name, | ||
Data: pm.Data, | ||
}, | ||
} | ||
return m.validate() | ||
} | ||
|
||
func (pf *PostMediaDefaultPointer) validate() error { | ||
return pf.PostMediaPointer.validate() | ||
} | ||
|
||
func (m *PostDefaultMedia) validate() error { | ||
return m.PostMedia.validate() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package responses | ||
|
||
type GetAllDefaultMedia struct { | ||
Media []GetDefaultMedia `json:"media"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package responses | ||
|
||
import "github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain" | ||
|
||
type GetDefaultMedia struct { | ||
domain.MediaFile | ||
DefaultID int `json:"default_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package responses | ||
|
||
type PostDefaultMedia struct { | ||
ID int `json:"id"` | ||
MediaId int `json:"media_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.