Skip to content

Commit

Permalink
Added part of default media service
Browse files Browse the repository at this point in the history
  • Loading branch information
Impervguin committed Jul 12, 2024
1 parent 2ad3400 commit 2435c64
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 15 deletions.
48 changes: 47 additions & 1 deletion internal/app/mapper/media.go
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,
}
}
50 changes: 50 additions & 0 deletions internal/app/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/app/mapper"
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain"
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain/requests"
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain/responses"
"github.com/sirupsen/logrus"
Expand All @@ -13,7 +14,12 @@ import (

type mediaStorage interface {
PutMediaFile(ctx context.Context, name string, key string, data []byte) (int, error)
GetMediaFile(_ context.Context, id int) (*domain.MediaFile, error)
GetMediaFiles(_ context.Context, ids []int) (map[int]domain.MediaFile, error)
ClearUpMedia(ctx context.Context, logger *logrus.Logger) error
GetDefaultMedia(ctx context.Context, id int) (*domain.DefaultMedia, error)
GetAllDefaultMedia(ctx context.Context) ([]domain.DefaultMedia, error)
PutDefaultMedia(ctx context.Context, name string, key string, data []byte) (id int, mediaId int, err error)
}

type MediaService struct {
Expand Down Expand Up @@ -54,3 +60,47 @@ func (s *MediaService) PostMediaBcrypt(ctx context.Context, req *requests.PostMe
func (s *MediaService) ClearUpMedia(ctx context.Context, logger *logrus.Logger) error {
return s.storage.ClearUpMedia(ctx, logger)
}

func (s *MediaService) GetMediaDefault(ctx context.Context, ID int) (*responses.GetDefaultMedia, error) {
defaultMedia, err := s.storage.GetDefaultMedia(ctx, ID)
if err != nil {
return nil, fmt.Errorf("can't storage.GetDefaultMedia: %w", err)
}

media, err := s.storage.GetMediaFile(ctx, defaultMedia.MediaID)
if err != nil {
return nil, fmt.Errorf("can't storage.GetMediaFile: %w", err)
}

return mapper.MakeResponseGetDefaultMedia(defaultMedia, media)
}

func (s *MediaService) GetAllMediaDefault(ctx context.Context) (*responses.GetAllDefaultMedia, error) {
defaultMedias, err := s.storage.GetAllDefaultMedia(ctx)
if err != nil {
return nil, fmt.Errorf("can't storage.GetAllDefaultMedia: %w", err)
}

if len(defaultMedias) == 0 {
return nil, fmt.Errorf("no default media")
}
ids := make([]int, 0, len(defaultMedias))
for _, defaultMedia := range defaultMedias {
ids = append(ids, defaultMedia.MediaID)
}

mediaFiles, err := s.storage.GetMediaFiles(ctx, ids)
if err != nil {
return nil, fmt.Errorf("can't storage.GetMediaFiles: %w", err)
}

return mapper.MakeResponseAllDefaultMedia(defaultMedias, mediaFiles)
}

func (s *MediaService) PutMediaDefault(ctx context.Context, name string, data []byte) (*responses.PostDefaultMedia, error) {
id, mediaID, err := s.storage.PutDefaultMedia(ctx, name, name, data)
if err != nil {
return nil, fmt.Errorf("can't storage.PutDefaultMedia: %v", err)
}
return mapper.MakeResponsePostDefaultMedia(id, mediaID), nil
}
5 changes: 5 additions & 0 deletions internal/domain/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ type ClubPhoto struct {
ClubID int `json:"club_id"`
RefNumber int `json:"ref_number"`
}

type DefaultMedia struct {
ID int `json:"id"`
MediaID int `json:"media_id"`
}
30 changes: 30 additions & 0 deletions internal/domain/requests/get-default-media.go
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
}
46 changes: 46 additions & 0 deletions internal/domain/requests/post-default-media.go
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()
}
5 changes: 5 additions & 0 deletions internal/domain/responses/get-all-default-media.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package responses

type GetAllDefaultMedia struct {
Media []GetDefaultMedia `json:"media"`
}
8 changes: 8 additions & 0 deletions internal/domain/responses/get-default-media.go
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"`
}
6 changes: 6 additions & 0 deletions internal/domain/responses/post-default-media.go
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"`
}
48 changes: 45 additions & 3 deletions internal/infrastructure/postgres/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const getMediaFile = "SELECT name, key FROM mediafile WHERE id = $1 AND id > 0"
func (p *Postgres) GetMediaFile(id int) (*domain.MediaFile, error) {
f := domain.MediaFile{}
err := p.db.QueryRow(getMediaFile, id).Scan(&f.Name, &f.Key)
if err == nil {
return &f, nil
if err != nil {
return nil, err
}
f.ID = id
return nil, err
return &f, nil
}

const getMediaFiles = "SELECT id, name, key FROM mediafile WHERE id = ANY($1) AND id > 0"
Expand Down Expand Up @@ -120,3 +120,45 @@ func (p *Postgres) GetAllMediaKeys(ctx context.Context) ([]string, error) {
}
return keys, nil
}

const getDefautlMedia = "SELECT id, media_id FROM default_media WHERE id = $1"

func (p *Postgres) GetDefautlMedia(ctx context.Context, id int) (*domain.DefaultMedia, error) {
var d domain.DefaultMedia
err := p.db.QueryRow(getDefautlMedia, id).Scan(&d.ID, &d.MediaID)
if err == nil {
return &d, nil
}
return nil, err
}

const getAllDefaultMedia = "SELECT id, media_id FROM default_media"

func (p *Postgres) GetAllDefaultMedia(ctx context.Context) ([]domain.DefaultMedia, error) {
defaultMedia := make([]domain.DefaultMedia, 0)
rows, err := p.db.Query(getAllDefaultMedia)
if err != nil {
return nil, err
}

for rows.Next() {
d := domain.DefaultMedia{}
err := rows.Scan(&d.ID, &d.MediaID)
if err != nil {
return nil, err
}
defaultMedia = append(defaultMedia, d)
}

return defaultMedia, nil
}

const addDefaultMedia = "INSERT INTO default_media (media_id) VALUES ($1) RETURNING id"

func (p *Postgres) AddDefaultMedia(ctx context.Context, mediaID int) (int, error) {
err := p.db.QueryRow(addDefaultMedia, mediaID).Scan(&mediaID)
if err != nil {
return 0, wrapPostgresError(err.(pgx.PgError).Code, err)
}
return mediaID, nil
}
Loading

0 comments on commit 2435c64

Please sign in to comment.