Skip to content

Commit

Permalink
fixed create and edit bugs (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed May 25, 2023
1 parent 73cb7bc commit 2091b96
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
20 changes: 10 additions & 10 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ func (db database) CreateOrEditTribe(m Tribe) (Tribe, error) {
if m.Badges == nil {
m.Badges = []string{}
}
if err := db.db.Set("gorm:insert_option", onConflict).Create(&m).Error; err != nil {
fmt.Println(">>>>>>>>> == ", err)
return Tribe{}, err

if db.db.Model(&m).Where("uuid = ?", m.UUID).Updates(&m).RowsAffected == 0 {
db.db.Create(&m)
}

db.db.Exec(`UPDATE tribes SET tsv =
setweight(to_tsvector(name), 'A') ||
setweight(to_tsvector(description), 'B') ||
Expand Down Expand Up @@ -170,10 +171,11 @@ func (db database) CreateOrEditBot(b Bot) (Bot, error) {
if b.Tags == nil {
b.Tags = []string{}
}
if err := db.db.Set("gorm:insert_option", onConflict).Create(&b).Error; err != nil {
fmt.Println(err)
return Bot{}, err

if db.db.Model(&b).Where("uuid = ?", b.UUID).Updates(&b).RowsAffected == 0 {
db.db.Create(&b)
}

db.db.Exec(`UPDATE bots SET tsv =
setweight(to_tsvector(name), 'A') ||
setweight(to_tsvector(description), 'B') ||
Expand Down Expand Up @@ -210,10 +212,8 @@ func (db database) CreateOrEditPerson(m Person) (Person, error) {
m.GithubIssues = map[string]interface{}{}
}

if err := db.db.Model(&m).Where("id = ?", m.ID).UpdateColumns(m).Error; err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
db.db.Create(&m)
}
if db.db.Model(&m).Where("id = ?", m.ID).Updates(&m).RowsAffected == 0 {
db.db.Create(&m)
}

db.db.Exec(`UPDATE people SET tsv =
Expand Down
2 changes: 1 addition & 1 deletion db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Bot struct {
Name string `json:"name"`
UniqueName string `json:"unique_name"`
Description string `json:"description"`
Tags pq.StringArray `json:"tags"`
Tags pq.StringArray `gorm:"type:text[]" json:"tags"`
Img string `json:"img"`
PricePerUse int64 `json:"price_per_use"`
Created *time.Time `json:"created"`
Expand Down
6 changes: 0 additions & 6 deletions handlers/bots.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ func CreateOrEditBot(w http.ResponseWriter, r *http.Request) {
}
}

// existing := DB.getTribe(tribe.UUID)
// if existing.UUID != "" {
// w.WriteHeader(http.StatusUnauthorized)
// return
// }

bot.OwnerPubKey = extractedPubkey
bot.Updated = &now
bot.UniqueName, _ = BotUniqueNameFromName(bot.Name)
Expand Down

0 comments on commit 2091b96

Please sign in to comment.