diff --git a/internal/infrastructure/postgres/club.go b/internal/infrastructure/postgres/club.go index 0f26d98..2057d9c 100644 --- a/internal/infrastructure/postgres/club.go +++ b/internal/infrastructure/postgres/club.go @@ -17,7 +17,7 @@ const getClub = `SELECT parent_id, vk_url, tg_url - FROM club WHERE id = $1 + FROM club WHERE id = $1 AND id > 0 ` const NoParentClubID = 0 @@ -58,6 +58,7 @@ const getAllClub = `SELECT vk_url, tg_url FROM club +WHERE id > 0 ` func (s *Postgres) GetAllClub(_ context.Context) ([]domain.Club, error) { @@ -96,7 +97,7 @@ func (s *Postgres) GetAllClub(_ context.Context) ([]domain.Club, error) { return carr, nil } -const getClubsByName = `SELECT id, name, short_name, description, type, logo, parent_id, vk_url, tg_url FROM club WHERE name ILIKE $1` +const getClubsByName = `SELECT id, name, short_name, description, type, logo, parent_id, vk_url, tg_url FROM club WHERE name ILIKE $1 AND id > 0` func (s *Postgres) GetClubsByName(_ context.Context, name string) ([]domain.Club, error) { carr := []domain.Club{} @@ -134,7 +135,7 @@ func (s *Postgres) GetClubsByName(_ context.Context, name string) ([]domain.Club return carr, nil } -const getClubsByType = `SELECT id, name, short_name, description, type, logo, parent_id, vk_url, tg_url FROM club WHERE type ILIKE $1` +const getClubsByType = `SELECT id, name, short_name, description, type, logo, parent_id, vk_url, tg_url FROM club WHERE type ILIKE $1 AND id > 0` func (s *Postgres) GetClubsByType(_ context.Context, type_ string) ([]domain.Club, error) { carr := []domain.Club{} @@ -210,7 +211,7 @@ JOIN FROM club ) as clubs ON (club_org.club_id = clubs.id) -WHERE club_id = $1 +WHERE club_id = $1 AND club_id > 0 ` func (s *Postgres) GetClubOrgs(_ context.Context, clubID int) ([]domain.ClubOrg, error) { @@ -282,7 +283,7 @@ JOIN FROM club ) as clubs ON (club_org.club_id = clubs.id) -WHERE club_id = ANY($1) +WHERE club_id = ANY($1) AND club_id > 0 ` func (s *Postgres) GetClubsOrgs(_ context.Context, clubIDs []int) ([]domain.ClubOrg, error) { @@ -352,9 +353,10 @@ JOIN id, name FROM club + WHERE id > 0 ) as clubs ON (club_org.club_id = clubs.id) -WHERE club_id = ANY((SELECT id FROM club WHERE parent_id = $1)) +WHERE club_id = ANY((SELECT id FROM club WHERE parent_id = $1)) AND club_id > 0 ` func (s *Postgres) GetClubSubOrgs(_ context.Context, clubID int) ([]domain.ClubOrg, error) { @@ -425,6 +427,7 @@ JOIN id, name FROM club + WHERE id > 0 ) as clubs ON (club_org.club_id = clubs.id) ` @@ -527,7 +530,7 @@ SELECT club_id, media_id FROM club_photo -WHERE club_id = $1 +WHERE club_id = $1 AND club_id > 0 ` func (s *Postgres) GetClubMediaFiles(clubID int) ([]domain.ClubPhoto, error) { @@ -562,12 +565,6 @@ func (s *Postgres) DeleteClubWithOrgs(_ context.Context, clubID int) error { return err } - _, err = tx.Exec(deleteClub, clubID) - if err != nil { - tx.Rollback() - return err - } - _, err = tx.Exec(deleteClubOrgs, clubID) if err != nil { tx.Rollback() @@ -598,6 +595,12 @@ func (s *Postgres) DeleteClubWithOrgs(_ context.Context, clubID int) error { return err } + _, err = tx.Exec(deleteClub, clubID) + if err != nil { + tx.Rollback() + return err + } + return tx.Commit() } @@ -611,7 +614,7 @@ SET name=$1, parent_id=$6, vk_url=$7, tg_url=$8 -WHERE id = $9 +WHERE id = $9 AND id > 0 ` func (s *Postgres) UpdateClub(_ context.Context, c *domain.Club, o []domain.ClubOrg) error { diff --git a/internal/infrastructure/postgres/mediafile.go b/internal/infrastructure/postgres/mediafile.go index 27a9774..6326c50 100644 --- a/internal/infrastructure/postgres/mediafile.go +++ b/internal/infrastructure/postgres/mediafile.go @@ -7,7 +7,7 @@ import ( "github.com/jackc/pgx" ) -const getMediaFile = "SELECT name, key FROM mediafile WHERE id = $1" +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{} @@ -19,7 +19,7 @@ func (p *Postgres) GetMediaFile(id int) (*domain.MediaFile, error) { return nil, err } -const getMediaFiles = "SELECT id, name, key FROM mediafile WHERE id = ANY($1)" +const getMediaFiles = "SELECT id, name, key FROM mediafile WHERE id = ANY($1) AND id > 0" func (p *Postgres) GetMediaFiles(ids []int) (map[int]domain.MediaFile, error) { m := make(map[int]domain.MediaFile) @@ -49,7 +49,7 @@ func (p *Postgres) AddMediaFile(name, key string) (int, error) { return id, nil } -const deleteMediaFile = "DELETE FROM mediafile WHERE id = $1" +const deleteMediaFile = "DELETE FROM mediafile WHERE id = $1 AND id > 0" func (p *Postgres) DeleteMediaFile(id int) error { _, err := p.db.Exec(deleteMediaFile, id) @@ -72,7 +72,7 @@ WHERE id NOT IN ( SELECT media_id FROM event UNION ALL SELECT logo as media_id FROM club -) +) AND id > 0 ` func (p *Postgres) GetUnusedMedia(ctx context.Context) ([]domain.MediaFile, error) { @@ -93,14 +93,14 @@ func (p *Postgres) GetUnusedMedia(ctx context.Context) ([]domain.MediaFile, erro return res, nil } -const deleteMediaFiles = "DELETE FROM mediafile WHERE key = ANY($1)" +const deleteMediaFiles = "DELETE FROM mediafile WHERE key = ANY($1) AND id > 0" func (p *Postgres) DeleteMediaFiles(ctx context.Context, keys []string) error { _, err := p.db.Exec(deleteMediaFiles, keys) return err } -const getAllMediaKeys = "SELECT key FROM mediafile" +const getAllMediaKeys = "SELECT key FROM mediafile WHERE id > 0" func (p *Postgres) GetAllMediaKeys(ctx context.Context) ([]string, error) { rows, err := p.db.Query(getAllMediaKeys) diff --git a/internal/ports/media.go b/internal/ports/media.go index 5cc9ea5..1889d3e 100644 --- a/internal/ports/media.go +++ b/internal/ports/media.go @@ -40,6 +40,11 @@ func (h *MediaHandler) Routes() chi.Router { r.Post("/public", h.r.Wrap(h.PostMediaPublic)) r.Post("/private", h.r.Wrap(h.PostMediaPrivate)) + r.Get("/default", h.r.Wrap(h.GetMediaDefault)) + r.Get("/default/{id}", h.r.Wrap(h.GetMediaDefaultByID)) + r.Post("/default", h.r.Wrap(h.PostMediaDefault)) + r.Delete("/default", h.r.Wrap(h.DeleteMediaDefault)) + r.Put("/default/{id}", h.r.Wrap(h.PutMediaDefault)) return r } @@ -147,3 +152,23 @@ func (h *MediaHandler) PostMediaPrivate(w http.ResponseWriter, req *http.Request return handler.OkResponse(response) } + +func (h *MediaHandler) GetMediaDefault(w http.ResponseWriter, req *http.Request) handler.Response { + return nil +} + +func (h *MediaHandler) GetMediaDefaultByID(w http.ResponseWriter, req *http.Request) handler.Response { + return nil +} + +func (h *MediaHandler) PostMediaDefault(w http.ResponseWriter, req *http.Request) handler.Response { + return nil +} + +func (h *MediaHandler) DeleteMediaDefault(w http.ResponseWriter, req *http.Request) handler.Response { + return nil +} + +func (h *MediaHandler) PutMediaDefault(w http.ResponseWriter, req *http.Request) handler.Response { + return nil +} diff --git a/migrations/014_init_default_media_table.sql b/migrations/014_init_default_media_table.sql new file mode 100644 index 0000000..edb19f2 --- /dev/null +++ b/migrations/014_init_default_media_table.sql @@ -0,0 +1,15 @@ +-- +goose Up +-- +goose StatementBegin +create table IF NOT EXISTS default_media +( + id serial PRIMARY KEY, + media_id int not null, + FOREIGN KEY (media_id) REFERENCES mediafile(id) +); + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +drop table IF EXISTS default_media; +-- +goose StatementEnd