Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Jul 7, 2016
1 parent 3432d0e commit 3aa5b12
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 1 addition & 7 deletions api/clan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package api

import (
"fmt"
"strings"

"github.com/kataras/iris"
Expand Down Expand Up @@ -163,16 +162,11 @@ func LeaveClanHandler(app *App) func(c *iris.Context) {
return
}

clan, err := models.GetClanByPublicID(db, gameID, publicID)
clan, newOwner, err := models.GetClanAndOwnerByPublicID(db, gameID, publicID)
if err != nil {
FailWith(500, (&models.ModelNotFoundError{"Clan", publicID}).Error(), c)
return
}
newOwner, err := models.GetPlayerByID(db, clan.OwnerID)
if err != nil {
FailWith(500, fmt.Sprintf("Could not find new owner for clan %s.", clan.Name), c)
return
}

newOwnerJSON := newOwner.Serialize()
delete(newOwnerJSON, "gameID")
Expand Down
14 changes: 14 additions & 0 deletions models/clan.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,17 @@ func SearchClan(db DB, gameID, term string) ([]Clan, error) {

return clans, nil
}

// GetClanAndOwnerByPublicID returns the clan as well as the owner of a clan by clan's public id
func GetClanAndOwnerByPublicID(db DB, gameID, publicID string) (*Clan, *Player, error) {
clan, err := GetClanByPublicID(db, gameID, publicID)
if err != nil {
return nil, nil, err
}
newOwner, err := GetPlayerByID(db, clan.OwnerID)
if err != nil {
return nil, nil, err
}

return clan, newOwner, nil
}
13 changes: 13 additions & 0 deletions models/clan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,5 +746,18 @@ func TestClanModel(t *testing.T) {
g.Assert(err.Error()).Equal("A search term was not provided to find a clan.")
})
})

g.Describe("Get Clan and Owner", func() {
g.It("Should return clan and owner", func() {
_, clan, owner, _, _, err := GetClanWithMemberships(
testDb, 10, 3, 4, 5, "", "",
)
g.Assert(err == nil).IsTrue()

dbClan, dbOwner, err := GetClanAndOwnerByPublicID(testDb, clan.GameID, clan.PublicID)
g.Assert(dbClan.ID).Equal(clan.ID)
g.Assert(dbOwner.ID).Equal(owner.ID)
})
})
})
}

0 comments on commit 3aa5b12

Please sign in to comment.