Skip to content

Commit

Permalink
add get and update calls for characters
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 9, 2020
1 parent 5b8d816 commit 28e6e37
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
44 changes: 43 additions & 1 deletion internal/server/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"regexp"
"strconv"

"github.com/cfi2017/bl3-save/internal/shared"
"github.com/cfi2017/bl3-save/pkg/character"
"github.com/cfi2017/bl3-save/pkg/pb"
"github.com/gin-gonic/gin"
)

var (
charPattern = regexp.MustCompile("(\\d+)\\.sav")
)

func ListCharacters(c *gin.Context) {
func listCharacters(c *gin.Context) {
files, err := ioutil.ReadDir(pwd)
if err != nil {
c.AbortWithStatus(500)
Expand All @@ -35,6 +37,46 @@ func ListCharacters(c *gin.Context) {

}

func getCharacter(c *gin.Context) {
id := c.Param("id")

f, err := os.Open(pwd + "/" + id + ".sav")
if err != nil {
c.AbortWithStatus(500)
return
}
defer f.Close()
s, char := character.Deserialize(f)
c.JSON(200, struct {
Save shared.SavFile `json:"save"`
Character pb.Character `json:"character"`
}{Save: s, Character: char})

}

func updateCharacter(c *gin.Context) {
id := c.Param("id")

var d struct {
Save shared.SavFile `json:"save"`
Character pb.Character `json:"character"`
}
err := c.BindJSON(&d)
if err != nil {
c.AbortWithStatus(500)
return
}
f, err := os.Create(pwd + "/" + id + ".sav")
if err != nil {
c.AbortWithStatus(500)
return
}
defer f.Close()
character.Serialize(f, d.Save, d.Character)
c.Status(204)
return
}

type CharInfo struct {
ID int `json:"id"`
Name string `json:"name"`
Expand Down
6 changes: 3 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func Start() error {
r.GET("/profile", getProfile)
r.POST("/profile", updateProfile)

r.GET("/characters", ListCharacters)
r.GET("/characters/:id")
r.POST("/characters/:id")
r.GET("/characters", listCharacters)
r.GET("/characters/:id", getCharacter)
r.POST("/characters/:id", updateCharacter)

return r.Run(":5050")
}

0 comments on commit 28e6e37

Please sign in to comment.