Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rolevents #21

Merged
merged 24 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 96 additions & 23 deletions internal/app/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/STUD-IT-team/bmstu-stud-web-backend/internal/infrastructure/postgres"
pai0id marked this conversation as resolved.
Show resolved Hide resolved
)

type clubStorage interface {
Expand All @@ -26,6 +27,10 @@ type clubStorage interface {
AddOrgs(ctx context.Context, orgs []domain.ClubOrg) error
DeleteClubWithOrgs(ctx context.Context, clubID int) error
UpdateClub(ctx context.Context, c *domain.Club, o []domain.ClubOrg) error
AddClubPhotos(ctx context.Context, p []domain.ClubPhoto) error
DeleteClubPhoto(ctx context.Context, ids int) error
GetPhotoClubID(ctx context.Context, photoID int) (int, error)
UpdateClubPhotos(ctx context.Context, clubID int, photos []domain.ClubPhoto) error
}

type ClubService struct {
Expand All @@ -47,14 +52,22 @@ func (s *ClubService) GetClub(ctx context.Context, id int) (*responses.GetClub,

mainOrgs, err := s.storage.GetClubOrgs(ctx, id)
if err != nil {
err = fmt.Errorf("can't storage.GetClubOrgs: %w", err)
return nil, err
if err == postgres.ErrPostgresNotFoundError {
mainOrgs = []domain.ClubOrg{}
} else {
err = fmt.Errorf("can't storage.GetClubOrgs: %w", err)
return nil, err
}
}

subOrgs, err := s.storage.GetClubSubOrgs(ctx, id)
if err != nil {
err = fmt.Errorf("can't storage.GetClubSubOrgs: %w", err)
return nil, err
if err == postgres.ErrPostgresNotFoundError {
subOrgs = []domain.ClubOrg{}
} else {
pai0id marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("can't storage.GetClubSubOrgs: %w", err)
return nil, err
}
}

ids := make([]int, 0, len(mainOrgs)+len(subOrgs)+1)
Expand All @@ -72,7 +85,7 @@ func (s *ClubService) GetClub(ctx context.Context, id int) (*responses.GetClub,
return nil, err
}

return mapper.MakeResponseClub(club, &mainOrgs, &subOrgs, &ims)
return mapper.MakeResponseClub(club, mainOrgs, subOrgs, ims)
}

func (s *ClubService) GetClubsByName(ctx context.Context, name string) (*responses.GetClubsByName, error) {
Expand All @@ -82,10 +95,6 @@ func (s *ClubService) GetClubsByName(ctx context.Context, name string) (*respons
return nil, err
}

if len(res) == 0 {
return nil, fmt.Errorf("no club found")
}

ids := make([]int, 0, len(res))
for _, club := range res {
ids = append(ids, club.LogoId)
Expand Down Expand Up @@ -114,10 +123,6 @@ func (s *ClubService) GetClubsByType(ctx context.Context, type_ string) (*respon
return nil, err
}

if len(res) == 0 {
return nil, fmt.Errorf("no club found")
}

ids := make([]int, 0, len(res))
for _, club := range res {
ids = append(ids, club.LogoId)
Expand Down Expand Up @@ -147,10 +152,6 @@ func (s *ClubService) GetAllClubs(ctx context.Context) (*responses.GetAllClubs,
return nil, err
}

if len(res) == 0 {
return nil, fmt.Errorf("no club found")
}

ids := make([]int, 0, len(res))
for _, club := range res {
ids = append(ids, club.LogoId)
Expand All @@ -173,12 +174,22 @@ func (s *ClubService) GetAllClubs(ctx context.Context) (*responses.GetAllClubs,
func (s *ClubService) GetClubMembers(ctx context.Context, clubID int) (*responses.GetClubMembers, error) {
orgs, err := s.storage.GetClubOrgs(ctx, clubID)
if err != nil {
return nil, fmt.Errorf("can't storage.GetClubOrgs: %w", err)
if err == postgres.ErrPostgresNotFoundError {
orgs = []domain.ClubOrg{}
} else {
err = fmt.Errorf("can't storage.GetClubOrgs: %w", err)
return nil, err
}
}

subOrgs, err := s.storage.GetClubSubOrgs(ctx, clubID)
if err != nil {
return nil, fmt.Errorf("can't storage.GetClubSubOrgs: %w", err)
if err == postgres.ErrPostgresNotFoundError {
subOrgs = []domain.ClubOrg{}
} else {
err = fmt.Errorf("can't storage.GetClubSubOrgs: %w", err)
return nil, err
}
}

if len(orgs)+len(subOrgs) == 0 {
Expand Down Expand Up @@ -247,12 +258,13 @@ func (s *ClubService) UpdateClub(ctx context.Context, req *requests.UpdateClub)
func (s *ClubService) GetClubMediaFiles(ctx context.Context, clubID int) (*responses.GetClubMedia, error) {
clubPhotos, err := s.storage.GetClubMediaFiles(ctx, clubID)
if err != nil {
return nil, fmt.Errorf("can't storage.GetClubMediaFiles: %w", err)
if err == postgres.ErrPostgresNotFoundError {
clubPhotos = []domain.ClubPhoto{}
} else {
return nil, fmt.Errorf("can't storage.GetClubMediaFiles: %w", err)
}
}

if len(clubPhotos) == 0 {
return nil, fmt.Errorf("no club photo found")
}
ids := make([]int, 0, len(clubPhotos))
for _, photo := range clubPhotos {
ids = append(ids, photo.MediaID)
Expand All @@ -264,3 +276,64 @@ func (s *ClubService) GetClubMediaFiles(ctx context.Context, clubID int) (*respo

return mapper.MakeResponseClubMediaFiles(clubID, clubPhotos, media)
}

func (s *ClubService) PostClubPhoto(ctx context.Context, req *requests.PostClubPhoto) error {
photos := make([]domain.ClubPhoto, 0, len(req.Photos))
for _, p := range req.Photos {
photos = append(photos, domain.ClubPhoto{
ClubID: req.ClubID,
MediaID: p.MediaID,
RefNumber: p.RefNumber,
ID: 0,
})
}

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

func (s *ClubService) DeleteClubPhoto(ctx context.Context, req *requests.DeleteClubPhoto) error {
clubID, err := s.storage.GetPhotoClubID(ctx, req.PhotoID)
if err != nil {
if err == postgres.ErrPostgresNotFoundError {
return fmt.Errorf("photo not found")
}
return fmt.Errorf("can't storage.GetPhotoClubID: %w", err)
}
if clubID != req.ClubID {
return fmt.Errorf("photo is not from the specified club")
}
err = s.storage.DeleteClubPhoto(ctx, req.PhotoID)
if err != nil {
return fmt.Errorf("can't storage.DeleteClubPhoto: %w", err)
}
return nil
}

func (s *ClubService) UpdateClubPhoto(ctx context.Context, req *requests.UpdateClubPhoto) error {
clubID := req.ClubID
photos := make([]domain.ClubPhoto, 0, len(req.Photos))
for _, p := range req.Photos {
photos = append(photos, domain.ClubPhoto{
ClubID: clubID,
MediaID: p.MediaID,
RefNumber: p.RefNumber,
})
}
err := s.storage.UpdateClubPhotos(ctx, clubID, photos)
if err != nil {
return fmt.Errorf("can't storage.UpdateClubPhotos: %w", err)
}
return err

}

func (s *ClubService) GetClearancePost(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) {
if resp.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
return &responses.GetClearance{Access: false, Comment: "only admins"}, nil
}
4 changes: 2 additions & 2 deletions internal/app/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *DocumentsService) GetDocumentsByClubID(ctx context.Context, clubID int)
}

func (s *DocumentsService) PostDocument(ctx context.Context, doc *requests.PostDocument) (*responses.PostDocument, error) {
var key = fmt.Sprintf("%d/%s", doc.ClubID, doc.Name)
var key = fmt.Sprintf("%d/%s", doc.CategoryID, doc.Name)

err := s.storage.PostDocument(ctx, doc.Name, key, doc.Data, doc.ClubID, doc.CategoryID)
if err != nil {
Expand All @@ -83,7 +83,7 @@ func (s *DocumentsService) DeleteDocument(ctx context.Context, id int) error {
}

func (s *DocumentsService) UpdateDocument(ctx context.Context, doc *requests.UpdateDocument) (*responses.UpdateDocument, error) {
var key = fmt.Sprintf("%d/%s", doc.ClubID, doc.Name)
var key = fmt.Sprintf("%d/%s", doc.CategoryID, doc.Name)

err := s.storage.UpdateDocument(ctx, doc.ID, doc.Name, key, doc.Data, doc.ClubID, doc.CategoryID)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/app/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func (s *EventsService) GetEventsByRange(ctx context.Context, from, to time.Time
return mapper.MakeResponseEventsByRange(res, eventMediaFiles)
}

// func (s *EventsService) GetEventMemberRoles(ctx context.Context, id int) (*responses.GetEventMemberRoles, error) {

// return mapper.MakeResponseEventMemberRoles(res)
// }

func (s *EventsService) PostEvent(ctx context.Context, event *domain.Event) error {
err := s.storage.PostEvent(ctx, event)
if err != nil {
Expand Down
43 changes: 28 additions & 15 deletions internal/app/mapper/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/STUD-IT-team/bmstu-stud-web-backend/internal/domain/responses"
)

func MakeMainOrg(org *domain.ClubOrg, images *map[int]domain.MediaFile) (*responses.MainOrg, error) {
if _, ok := (*images)[org.MediaID]; !ok {
func MakeMainOrg(org *domain.ClubOrg, images map[int]domain.MediaFile) (*responses.MainOrg, error) {
if _, ok := images[org.MediaID]; !ok {
return &responses.MainOrg{}, fmt.Errorf("can't find image for org id: %v", org.MediaID)
}

Expand All @@ -20,12 +20,12 @@ func MakeMainOrg(org *domain.ClubOrg, images *map[int]domain.MediaFile) (*respon
TgUrl: org.Telegram,
Spec: org.RoleSpec,
RoleName: org.RoleName,
Image: (*images)[org.MediaID],
Image: images[org.MediaID],
}, nil
}

func MakeSubOrg(org *domain.ClubOrg, images *map[int]domain.MediaFile) (*responses.SubClubOrg, error) {
if _, ok := (*images)[org.MediaID]; !ok {
func MakeSubOrg(org *domain.ClubOrg, images map[int]domain.MediaFile) (*responses.SubClubOrg, error) {
if _, ok := images[org.MediaID]; !ok {
return &responses.SubClubOrg{}, fmt.Errorf("can't find image for org id: %v", org.MediaID)
}

Expand All @@ -36,36 +36,38 @@ func MakeSubOrg(org *domain.ClubOrg, images *map[int]domain.MediaFile) (*respons
VkUrl: org.Vk,
TgUrl: org.Telegram,
Spec: org.RoleSpec,
Image: (*images)[org.MediaID],
Image: images[org.MediaID],
}, nil

}

func MakeResponseClub(club *domain.Club, mainOrgs *[]domain.ClubOrg, subOrgs *[]domain.ClubOrg, images *map[int]domain.MediaFile) (*responses.GetClub, error) {
if _, ok := (*images)[club.LogoId]; !ok {
func MakeResponseClub(club *domain.Club, mainOrgs []domain.ClubOrg, subOrgs []domain.ClubOrg, images map[int]domain.MediaFile) (*responses.GetClub, error) {
if _, ok := images[club.LogoId]; !ok {
return &responses.GetClub{}, fmt.Errorf("can't get logo media for club id: %v", club.LogoId)
}

r := &responses.GetClub{
ID: club.ID,
Name: club.Name,
ShortName: club.ShortName,
Logo: (*images)[club.LogoId],
Logo: images[club.LogoId],
Description: club.Description,
Type: club.Type,
VkUrl: club.VkUrl,
TgUrl: club.TgUrl,
MainOrgs: make([]responses.MainOrg, 0),
SubOrgs: make([]responses.SubClubOrg, 0),
}

for _, org := range *mainOrgs {
for _, org := range mainOrgs {
m, err := MakeMainOrg(&org, images)
if err != nil {
return nil, err
}
r.MainOrgs = append(r.MainOrgs, *m)
}

for _, org := range *subOrgs {
for _, org := range subOrgs {
s, err := MakeSubOrg(&org, images)
if err != nil {
return nil, err
Expand All @@ -89,6 +91,12 @@ func MakeResponseAllClub(clubs []domain.Club, logos map[int]domain.MediaFile, or
orgMap[org.ClubID] = append(orgMap[org.ClubID], o)
}

for _, club := range clubs {
if _, ok := orgMap[club.ID]; !ok {
orgMap[club.ID] = []responses.ClubOrg{}
}
}

for _, club := range clubs {
if _, ok := logos[club.LogoId]; !ok {
return nil, fmt.Errorf("can't find logo for club id %v", logos)
Expand All @@ -111,19 +119,21 @@ func MakeResponseAllClub(clubs []domain.Club, logos map[int]domain.MediaFile, or

func MakeResponseClubMembers(clubID int, mainOrgs []domain.ClubOrg, subOrgs []domain.ClubOrg, images map[int]domain.MediaFile) (*responses.GetClubMembers, error) {
r := &responses.GetClubMembers{
ID: clubID,
ID: clubID,
MainOrgs: []responses.MainOrg{},
SubOrgs: []responses.SubClubOrg{},
}

for _, org := range mainOrgs {
m, err := MakeMainOrg(&org, &images)
m, err := MakeMainOrg(&org, images)
if err != nil {
return nil, err
}
r.MainOrgs = append(r.MainOrgs, *m)
}

for _, org := range subOrgs {
s, err := MakeSubOrg(&org, &images)
s, err := MakeSubOrg(&org, images)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +170,10 @@ func ParsePostClub(req *requests.PostClub) (*domain.Club, []domain.ClubOrg, erro
}

func MakeResponseClubMediaFiles(clubID int, clubPhotos []domain.ClubPhoto, media map[int]domain.MediaFile) (*responses.GetClubMedia, error) {
r := &responses.GetClubMedia{ID: clubID}
r := &responses.GetClubMedia{
ID: clubID,
Media: []responses.ClubMedia{},
}
for _, f := range clubPhotos {
media, ok := media[f.MediaID]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions internal/app/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *MediaService) PostMedia(ctx context.Context, req *requests.PostMedia) (
return &responses.PostMedia{}, fmt.Errorf("can't storage.PutMedia: %v", err)
}

return mapper.MakeResponsePostMedia(id), err
return mapper.MakeResponsePostMedia(id), nil
}

const bcryptCost = 12
Expand All @@ -56,7 +56,7 @@ func (s *MediaService) PostMediaBcrypt(ctx context.Context, req *requests.PostMe
return &responses.PostMedia{}, fmt.Errorf("can't storage.PutMedia: %v", err)
}

return mapper.MakeResponsePostMedia(id), fmt.Errorf("can't storage.UploadObject: %v", err)
return mapper.MakeResponsePostMedia(id), nil
}

func (s *MediaService) ClearUpMedia(ctx context.Context, logger *logrus.Logger) error {
Expand Down
6 changes: 6 additions & 0 deletions internal/domain/event-member-role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package domain

type EventMemberRole struct {
ID int `json:"id"`
Name string `json:"name"`
}
3 changes: 1 addition & 2 deletions internal/domain/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ type Member struct {
Telegram string `json:"telegram"`
Vk string `json:"vk"`
Name string `json:"name"`
RoleID int `json:"role_id"`
IsAdmin bool `json:"isAdmin"`
IsAdmin bool `json:"is_admin"`
}
Loading
Loading