Skip to content

Commit

Permalink
new table, in clubs and medifiles check for id > 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Impervguin committed Jul 12, 2024
1 parent 1430d34 commit 2ad3400
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
31 changes: 17 additions & 14 deletions internal/infrastructure/postgres/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -425,6 +427,7 @@ JOIN
id,
name
FROM club
WHERE id > 0
) as clubs
ON (club_org.club_id = clubs.id)
`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}

Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions internal/infrastructure/postgres/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions internal/ports/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions migrations/014_init_default_media_table.sql
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ad3400

Please sign in to comment.