Skip to content

Make clan tags unique (case sensitive) #103

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

Merged
merged 1 commit into from
Oct 13, 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
25 changes: 25 additions & 0 deletions db/clans.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ func GetClanByName(name string) (*Clan, error) {
return clan, nil
}

// GetClanByTag Gets a clan from the db by its tag (case-sensitive)
func GetClanByTag(tag string) (*Clan, error) {
var clan *Clan

result := SQL.
Preload("Stats").
Where("BINARY clans.tag = ?", tag).First(&clan)

if result.Error != nil {
return nil, result.Error
}

return clan, nil
}

// GetClansCount Gets the total amount of clans
func GetClansCount() (int, error) {
var count int
Expand Down Expand Up @@ -185,6 +200,16 @@ func DoesClanExistByName(name string) (bool, error) {
return clan != nil, nil
}

func DoesClanExistByTag(tag string) (bool, *Clan, error) {
clan, err := GetClanByTag(tag)

if err != nil && err != gorm.ErrRecordNotFound {
return false, nil, err
}

return clan != nil, clan, nil
}

// IsValidClanName Checks a string to see if it is a valid clan name
func IsValidClanName(name string) bool {
result, _ := regexp.MatchString("^[a-zA-Z0-9][a-zA-Z0-9 ]{2,29}$", name)
Expand Down
25 changes: 22 additions & 3 deletions handlers/clans.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
errClanFavoriteModeInvalid string = "Your clan `favorite_mode` must be a valid mode id."
errClanAccentColorInvalid string = "Your clan `accent_color` must be a valid hex code."
errClanNameExists string = "A clan with that name already exists. Please choose a different name."
errClanTagExists string = "A clan with that tag already exists. Please choose a different tag."
)

// CreateClan Creates a new clan if the user is eligible to.
Expand Down Expand Up @@ -52,16 +53,26 @@ func CreateClan(c *gin.Context) *APIError {
return APIErrorBadRequest(errClanTagInvalid)
}

exists, err := db.DoesClanExistByName(body.Name)
nameExists, err := db.DoesClanExistByName(body.Name)

if err != nil {
return APIErrorServerError("Error checking if clan exists by name", err)
}

if exists {
if nameExists {
return APIErrorBadRequest(errClanNameExists)
}

tagExists, _, err := db.DoesClanExistByTag(body.Tag)

if err != nil {
return APIErrorServerError("Error checking if clan exists by tag", err)
}

if tagExists {
return APIErrorBadRequest(errClanTagExists)
}

clan := db.Clan{
OwnerId: user.Id,
Name: body.Name,
Expand Down Expand Up @@ -191,7 +202,15 @@ func UpdateClan(c *gin.Context) *APIError {
return APIErrorBadRequest(errClanTagInvalid)
}

clan.Tag = *body.Tag
tagExists, existingClan, err := db.DoesClanExistByTag(*body.Tag)

if err != nil {
return APIErrorServerError("Error checking if clan exists by tag", err)
}

if tagExists && existingClan.Id != clan.Id {
return APIErrorBadRequest(errClanTagExists)
}

if err := clan.UpdateTag(*body.Tag); err != nil {
return APIErrorServerError("Error updating clan tag", err)
Expand Down
Loading