Skip to content

Commit

Permalink
Rationalize billboard updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamH18 committed Oct 29, 2023
1 parent 44dccb5 commit 12a2efc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 82 deletions.
1 change: 1 addition & 0 deletions bot/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func DiscordCommand(cmds []*discordgo.ApplicationCommand, n string) bool {
return false
}

// TODO: Add series billboard to creation
// Creates new channels for a series
func CreateChannels(ser database.Series) error {
//Get registered bounds for series channels
Expand Down
26 changes: 13 additions & 13 deletions database/dbrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (r *SQLiteRepository) AddReminder(rem Reminder) error {

// Add series entry to DB
func (r *SQLiteRepository) AddSeries(ser Series) error {
_, err := r.SeriesExec(ser.Guild, "INSERT INTO series(name_sh, name_full, guild, ping_role, repo_link) values(?, ?, ?, ?, ?)", strings.ToLower(ser.NameSh), ser.NameFull, ser.Guild, ser.PingRole, ser.RepoLink)
_, err := r.SeriesExec(ser.Guild, ser.NameSh, "INSERT INTO series(name_sh, name_full, guild, ping_role, repo_link) values(?, ?, ?, ?, ?)", strings.ToLower(ser.NameSh), ser.NameFull, ser.Guild, ser.PingRole, ser.RepoLink)

if err != nil {
return err
Expand Down Expand Up @@ -115,7 +115,7 @@ func (r *SQLiteRepository) AddSeriesNote(ser SeriesNote) error {

// Add roles billboard entry to DB
func (r *SQLiteRepository) AddSeriesBillboard(bb SeriesBB) error {
_, err := r.RolesBillboardsExec("INSERT INTO series_billboards(series, guild, channel, message) values(?, ?, ?, ?)", bb.Series, bb.Guild, bb.Channel, bb.Message)
_, err := r.SeriesBillboardsExec("INSERT INTO series_billboards(series, guild, channel, message) values(?, ?, ?, ?)", bb.Series, bb.Guild, bb.Channel, bb.Message)

if err != nil {
return err
Expand All @@ -137,7 +137,7 @@ func (r *SQLiteRepository) AddRolesBillboard(bb JobBB) error {

// Add colors billboard entry to DB
func (r *SQLiteRepository) AddColorsBillboard(bb ColorBB) error {
_, err := r.RolesBillboardsExec("INSERT INTO colors_billboards(guild, channel, message) values(?, ?, ?)", bb.Guild, bb.Channel, bb.Message)
_, err := r.ColorsBillboardsExec("INSERT INTO colors_billboards(guild, channel, message) values(?, ?, ?)", bb.Guild, bb.Channel, bb.Message)

if err != nil {
return err
Expand All @@ -148,7 +148,7 @@ func (r *SQLiteRepository) AddColorsBillboard(bb ColorBB) error {

// Add assignment entry to DB
func (r *SQLiteRepository) AddAssignment(sea SeriesAssignment) error {
_, err := r.SeriesAssignmentsExec(sea.Guild, "INSERT INTO series_assignments(user, series, job, guild) values(?, ?, ?, ?)", sea.User, strings.ToLower(sea.Series), strings.ToLower(sea.Job), sea.Guild)
_, err := r.SeriesAssignmentsExec(sea.Guild, sea.Series, "INSERT INTO series_assignments(user, series, job, guild) values(?, ?, ?, ?)", sea.User, strings.ToLower(sea.Series), strings.ToLower(sea.Job), sea.Guild)

if err != nil {
return err
Expand Down Expand Up @@ -202,7 +202,7 @@ func (r *SQLiteRepository) RemoveUserReminder(id int64, userID string, guild str

// Remove series entry and all references to series in other tables
func (r *SQLiteRepository) RemoveSeries(nameSh string, nameFull string, guildId string) (bool, error) {
res, err := r.SeriesExec(guildId, "DELETE FROM series WHERE name_sh = ? AND name_full = ? AND guild = ?", nameSh, nameFull, guildId)
res, err := r.SeriesExec(guildId, nameSh, "DELETE FROM series WHERE name_sh = ? AND name_full = ? AND guild = ?", nameSh, nameFull, guildId)

if err != nil {
return false, err
Expand All @@ -217,7 +217,7 @@ func (r *SQLiteRepository) RemoveSeries(nameSh string, nameFull string, guildId
// If a series was removed, remove all references to it from other tables. Can't be assed to error check, not a big deal if this fails
if done {
r.ChannelsExec("DELETE FROM channels WHERE series = ? AND guild = ?", nameSh, guildId)

Check failure on line 219 in database/dbrepo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.ChannelsExec` is not checked (errcheck)
r.SeriesAssignmentsExec(guildId, "DELETE FROM series_assignments WHERE series = ? AND guild = ?", nameSh, guildId)
r.SeriesAssignmentsExec(guildId, nameSh, "DELETE FROM series_assignments WHERE series = ? AND guild = ?", nameSh, guildId)

Check failure on line 220 in database/dbrepo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.SeriesAssignmentsExec` is not checked (errcheck)
r.SeriesBillboardsExec("DELETE FROM series_billboards WHERE series = ? AND guild = ?", nameSh, guildId)

Check failure on line 221 in database/dbrepo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.SeriesBillboardsExec` is not checked (errcheck)
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func (r *SQLiteRepository) RemoveUser(userId string, guildId string) (bool, erro
// If a user was removed, remove all references to them from other tables. Can't be assed to error check, not a big deal if this fails
if done {
r.RemindersExec("DELETE FROM reminders WHERE user = ? AND guild = ?", userId, guildId)

Check failure on line 259 in database/dbrepo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.RemindersExec` is not checked (errcheck)
r.SeriesAssignmentsExec(guildId, "DELETE FROM series_assignments WHERE user = ? AND guild = ?", userId, guildId)
r.SeriesAssignmentsExec(guildId, "", "DELETE FROM series_assignments WHERE user = ? AND guild = ?", userId, guildId)

Check failure on line 260 in database/dbrepo.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `r.SeriesAssignmentsExec` is not checked (errcheck)
}

return done, nil
Expand All @@ -279,7 +279,7 @@ func (r *SQLiteRepository) RemoveJob(nameSh string, guildId string) (bool, error
done := rows > 0
// If a job was removed, remove all references to it from other tables. Can't be assed to error check, not a big deal if this fails
if done {
r.SeriesAssignmentsExec(guildId, "DELETE FROM series_assignments WHERE job = ? AND guild = ?", nameSh, guildId)
r.SeriesAssignmentsExec(guildId, "", "DELETE FROM series_assignments WHERE job = ? AND guild = ?", nameSh, guildId)
}

return done, nil
Expand All @@ -303,7 +303,7 @@ func (r *SQLiteRepository) RemoveMemberRole(guild string) (bool, error) {

// Remove series assignment
func (r *SQLiteRepository) RemoveSeriesAssignment(user string, series string, job string, guild string) (bool, error) {
res, err := r.SeriesAssignmentsExec(guild, "DELETE FROM series_assignments WHERE user = ? AND series = ? AND job = ? AND guild = ?", user, series, job, guild)
res, err := r.SeriesAssignmentsExec(guild, series, "DELETE FROM series_assignments WHERE user = ? AND series = ? AND job = ? AND guild = ?", user, series, job, guild)

if err != nil {
return false, err
Expand Down Expand Up @@ -335,7 +335,7 @@ func (r *SQLiteRepository) RemoveSeriesNote(series string, guild string, id int)

// Remove all assignments for a user
func (r *SQLiteRepository) RemoveAllAssignments(user string, guild string) (bool, error) {
res, err := r.SeriesAssignmentsExec(guild, "DELETE FROM series_assignments WHERE user = ? AND guild = ?", user, guild)
res, err := r.SeriesAssignmentsExec(guild, "", "DELETE FROM series_assignments WHERE user = ? AND guild = ?", user, guild)

if err != nil {
return false, err
Expand All @@ -351,7 +351,7 @@ func (r *SQLiteRepository) RemoveAllAssignments(user string, guild string) (bool

// Remove series billboard
func (r *SQLiteRepository) RemoveSeriesBillboard(series string, guild string) (bool, error) {
res, err := r.RolesBillboardsExec("DELETE FROM series_billboards WHERE series = ? AND guild = ?", series, guild)
res, err := r.SeriesBillboardsExec("DELETE FROM series_billboards WHERE series = ? AND guild = ?", series, guild)

if err != nil {
return false, err
Expand Down Expand Up @@ -399,7 +399,7 @@ func (r *SQLiteRepository) RemoveColorsBillboard(guild string) (bool, error) {

// Update series name
func (r *SQLiteRepository) UpdateSeriesName(nameSh string, newName string, guild string) (bool, error) {
res, err := r.SeriesExec(guild, "UPDATE series SET name_full = ? WHERE name_sh = ? AND guild = ?", newName, nameSh, guild)
res, err := r.SeriesExec(guild, nameSh, "UPDATE series SET name_full = ? WHERE name_sh = ? AND guild = ?", newName, nameSh, guild)

if err != nil {
return false, err
Expand All @@ -415,7 +415,7 @@ func (r *SQLiteRepository) UpdateSeriesName(nameSh string, newName string, guild

// Update series repo link
func (r *SQLiteRepository) UpdateSeriesRepoLink(nameSh string, newLink string, guild string) (bool, error) {
res, err := r.SeriesExec(guild, "UPDATE series SET repo_link = ? WHERE name_sh = ? AND guild = ?", newLink, nameSh, guild)
res, err := r.SeriesExec(guild, nameSh, "UPDATE series SET repo_link = ? WHERE name_sh = ? AND guild = ?", newLink, nameSh, guild)

if err != nil {
return false, err
Expand Down
76 changes: 7 additions & 69 deletions database/execwrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (r *SQLiteRepository) RemindersExec(query string, args ...any) (sql.Result,
}

// Changing the series table can affect series billboards (repo_link or name_full)
func (r *SQLiteRepository) SeriesExec(guild string, query string, args ...any) (sql.Result, error) {
func (r *SQLiteRepository) SeriesExec(guild string, series string, query string, args ...any) (sql.Result, error) {
res, err := r.SerielExec(query, args...)
//If query errored, return error
if err != nil {
Expand All @@ -52,38 +52,8 @@ func (r *SQLiteRepository) SeriesExec(guild string, query string, args ...any) (
if num == 0 {
//If nothing changed, just leave
return res, nil
} else if num > 1 {
//If more than one series changed, update all series billboards in guild
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
//If only one update, try to get last insertion value
id, err := res.LastInsertId()
if err != nil {
//If error, no clue why, so don't do anything (maybe deleted value? Behavior is not well documented)
log.Println("Error getting last insert ID: " + err.Error())
return res, nil
}
//Get name of single updated series
quer, err := r.db.Query("SELECT name_sh FROM series WHERE ROWID = ?", id)
if err != nil {
log.Println("Error getting data for last insert: " + err.Error())
return res, nil
}
defer quer.Close()
var series string
if quer.Next() {
err = quer.Scan(&series)
if err != nil {
log.Println("Failed to read last insert data: " + err.Error())
return res, nil
}
} else {
log.Println("Last insert value not found")
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
//If single series could be identified, update billboard
//Otherwise update billboard based on passed values
SeriesCh <- func() (string, string) { return guild, series }
return res, nil
}
Expand All @@ -105,59 +75,27 @@ func (r *SQLiteRepository) UsersExec(guild string, query string, args ...any) (s
}

// Changing the assignments table can affect assignments and series billboards
func (r *SQLiteRepository) SeriesAssignmentsExec(guild string, query string, args ...any) (sql.Result, error) {
func (r *SQLiteRepository) SeriesAssignmentsExec(guild string, series string, query string, args ...any) (sql.Result, error) {
res, err := r.SerielExec(query, args...)
if err != nil {
return res, err
}
//Certainly update assignments billboard
AssignmentsCh <- guild

num, err := res.RowsAffected()
if err != nil {
//No clue why this would error out, but just update everything
log.Println("Error getting number of affected rows: " + err.Error())
AssignmentsCh <- guild
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
if num == 0 {
//If nothing changed, just leave
return res, nil
} else if num > 1 {
//If more than one series changed, update all series billboards in guild
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
//If only one update, try to get last insertion value
id, err := res.LastInsertId()
if err != nil {
//If error, no clue why, so update everything (maybe deleted value? Behavior is not well documented)
log.Println("Error getting last insert ID: " + err.Error())
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
//Get name of single updated series. If anything goes wrong, just update everything
quer, err := r.db.Query("SELECT series FROM series_assignments WHERE ROWID = ?", id)
if err != nil {
log.Println("Error getting data for last insert: " + err.Error())
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
defer quer.Close()
var series string
if quer.Next() {
err = quer.Scan(&series)
if err != nil {
log.Println("Failed to read last insert data: " + err.Error())
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
} else {
log.Println("Last insert value not found")
SeriesCh <- func() (string, string) { return guild, "" }
return res, nil
}
//If single series could be identified, update billboard

//If something changed, update billboards
AssignmentsCh <- guild
SeriesCh <- func() (string, string) { return guild, series }
return res, nil
}
Expand Down

0 comments on commit 12a2efc

Please sign in to comment.