Skip to content

Commit

Permalink
Add support for adding/updating usergroup tags
Browse files Browse the repository at this point in the history
  • Loading branch information
auggod committed Feb 16, 2022
1 parent 7254631 commit 1919591
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 117 deletions.
2 changes: 1 addition & 1 deletion proto/user/tag.validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/user/user.validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

237 changes: 124 additions & 113 deletions proto/user/usergroup_messages.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proto/user/usergroup_messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ message UserGroupUpdateRequest {
optional string group_email = 9;
optional string owner_id = 10;
repeated Link links = 11;
repeated Tag tags = 12;

//optional StreetAddress address = 8;
//optional string owner_id = 7; // required

// repeated User followers = 9;
// repeated UserGroup members = 10;
// repeated UserGroup memberOfGroups = 11;
// repeated Tag tags = 13;

// Privacy privacy = 15;
// repeated RelatedUserGroup recommended_artists = 16;
Expand Down
7 changes: 7 additions & 0 deletions proto/user/usergroup_messages.validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions server/usergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,67 @@ func (s *Server) AddUserGroup(ctx context.Context, usergroup *pbUser.UserGroupCr
GroupEmail: usergroup.GroupEmail,
}

if usergroup.Tags != nil {
tags := make([]model.Tag, len(usergroup.Tags))
names := make([]string, len(usergroup.Tags))

for i := range usergroup.Tags {
tag := model.Tag{
Name: usergroup.Tags[i].Name,
Type: "genre",
}
tag.ID = uuid.Must(uuid.NewRandom())
names[i] = tag.Name
tags[i] = tag
}

existing := []model.Tag{}

// find existing tags
err := s.db.NewSelect().
Model(&existing).
Where("type = ? AND name IN (?)", "genre", bun.In(names)).
Scan(ctx)

if err != nil {
return nil, err
}

var result []uuid.UUID
var insert []model.Tag

for l := range tags {
var seen uuid.UUID

for e := range existing {
if existing[e].Name == tags[l].Name {
seen = existing[e].ID
break
}
}

if seen == uuid.Nil {
insert = append(insert, tags[l])
result = append(result, tags[l].ID)
} else {
result = append(result, seen)
}
}

if len(insert) > 0 {
_, err := s.db.
NewInsert().
Model(&insert).
Exec(ctx)

if err != nil {
return nil, err
}
}

newUserGroup.Tags = result
}

if usergroup.Links != nil {
uris := make([]string, len(usergroup.Links))
links := make([]model.Link, len(usergroup.Links))
Expand Down Expand Up @@ -213,6 +274,67 @@ func (s *Server) UpdateUserGroup(ctx context.Context, UserGroupUpdateRequest *pb
updatedUserGroupValues["banner"] = *UserGroupUpdateRequest.Banner
}

if UserGroupUpdateRequest.Tags != nil {
tags := make([]model.Tag, len(UserGroupUpdateRequest.Tags))
names := make([]string, len(UserGroupUpdateRequest.Tags))

for i := range UserGroupUpdateRequest.Tags {
tag := model.Tag{
Name: UserGroupUpdateRequest.Tags[i].Name,
Type: "genre",
}
tag.ID = uuid.Must(uuid.NewRandom())
names[i] = tag.Name
tags[i] = tag
}

existing := []model.Tag{}

// find existing tags
err := s.db.NewSelect().
Model(&existing).
Where("type = ? AND name IN (?)", "genre", bun.In(names)).
Scan(ctx)

if err != nil {
return nil, err
}

var result []uuid.UUID
var insert []model.Tag

for l := range tags {
var seen uuid.UUID

for e := range existing {
if existing[e].Name == tags[l].Name {
seen = existing[e].ID
break
}
}

if seen == uuid.Nil {
insert = append(insert, tags[l])
result = append(result, tags[l].ID)
} else {
result = append(result, seen)
}
}

if len(insert) > 0 {
_, err := s.db.
NewInsert().
Model(&insert).
Exec(ctx)

if err != nil {
return nil, err
}
}

updatedUserGroupValues["tags"] = result
}

if UserGroupUpdateRequest.Links != nil {
links := make([]model.Link, len(UserGroupUpdateRequest.Links))
uris := make([]string, len(UserGroupUpdateRequest.Links))
Expand Down
2 changes: 1 addition & 1 deletion statik/statik.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions third_party/OpenAPI/user/user.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,12 @@
"items": {
"$ref": "#/definitions/userLink"
}
},
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/userTag"
}
}
}
},
Expand Down

0 comments on commit 1919591

Please sign in to comment.