Skip to content

Commit

Permalink
character api added
Browse files Browse the repository at this point in the history
  • Loading branch information
iamananya committed May 29, 2023
1 parent b34b394 commit e11f69e
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
Binary file modified cmd/main/main
Binary file not shown.
42 changes: 42 additions & 0 deletions pkg/controllers/gacha-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var NewUser models.User
var NewCharacter models.Character

/*
TODO----(Task completed)
Expand Down Expand Up @@ -120,3 +121,44 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)

}

func CreateCharacter(w http.ResponseWriter, r *http.Request) {
character := &models.Character{}
requestBody, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.Unmarshal(requestBody, character)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// empty username

if character.Name == "" {
w.WriteHeader(http.StatusBadRequest)
return
}

c := character.CreateCharacter()

res, err := json.Marshal(c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write(res)
}

func GetCharacters(w http.ResponseWriter, r *http.Request) {
characters := models.GetAllCharacters()

res, _ := json.Marshal(characters)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(res)
}
52 changes: 50 additions & 2 deletions pkg/models/gacha.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,38 @@ var db *gorm.DB

type User struct {
gorm.Model
Name string `gorm:"type:varchar(30);size:30" json:"name"`
Token string `gorm:"type:char(30)" json:"token"`
Name string `gorm:"type:varchar(30);size:30" json:"name"`
Token string `gorm:"type:char(30)" json:"token"`
Characters []UserCharacter `gorm:"foreignKey:UserID" json:"characters"`
}
type Character struct {
gorm.Model
Name string `gorm:"type:varchar(30);size:30" json:"name"`
AttackPower int `gorm:"column:attack_power" json:"attack_power"`
Defense int `gorm:"column:defense" json:"defense"`
Speed int `json:"speed"`
HitPoints int `gorm:"column:hit_points" json:"hit_points"`
CriticalHitRate float64 `gorm:"column:critical_hit_rate" json:"critical_hit_rate"`
ElementalAffinity string `json:"elemental_affinity"`
Rarity string `json:"rarity"`
Synergy bool `json:"synergy"`
Evolution bool `json:"evolution"`
Users []UserCharacter `gorm:"foreignKey:CharacterID" json:"users"`
}

type UserCharacter struct {
gorm.Model
UserID uint `gorm:"index" json:"user_id"`
CharacterID uint `gorm:"index" json:"character_id"`
AttackPower int `gorm:"column:attack_power" json:"attack_power"`
Defense int `gorm:"column:defense" json:"defense"`
Speed int `json:"speed"`
HitPoints int `gorm:"column:hit_points" json:"hit_points"`
CriticalHitRate float64 `gorm:"column:critical_hit_rate" json:"critical_hit_rate"`
ElementalAffinity string `json:"elemental_affinity"`
Rarity string `json:"rarity"`
Synergy bool `json:"synergy"`
Evolution bool `json:"evolution"`
}

func init() {
Expand All @@ -22,6 +52,9 @@ func init() {
db.AutoMigrate(&User{})
db.Model(&User{}).ModifyColumn("name", "varchar(30)")
db.Model(&User{}).ModifyColumn("token", "char(30)")
db.AutoMigrate(&Character{})
db.Model(&Character{}).ModifyColumn("name", "varchar(30)")

}

func (u *User) CreateUser() *User {
Expand Down Expand Up @@ -56,6 +89,21 @@ func GetUserById(Id int64) (*User, *gorm.DB) {
return &getUser, db
}

func (c *Character) CreateCharacter() *Character {
db.Create(&c)
return c
}

func GetAllCharacters() []Character {
var Characters []Character
db.Find(&Characters)
return Characters
}
func (uc *UserCharacter) CreateUserCharacter() *UserCharacter {
db.Create(&uc)
return uc
}

// func DeleteUser(Id int64) User {
// var user User
// db.Where("ID=?", Id).Delete(user)
Expand Down
4 changes: 4 additions & 0 deletions pkg/routes/gacha-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ var RegisterUserRoute = func(router *mux.Router) {
router.HandleFunc("/user/", controllers.GetUser).Methods("GET")
router.HandleFunc("/user/{userId}", controllers.GetUserById).Methods("GET")
router.HandleFunc("/user/{userId}", controllers.UpdateUser).Methods("PUT")
router.HandleFunc("/characters/", controllers.CreateCharacter).Methods("POST")
router.HandleFunc("/characters/list/", controllers.GetCharacters).Methods("GET")
// router.HandleFunc("/user/characters/", controllers.CreateUserCharacter).Methods("POST")

// router.HandleFunc("/user/{userId}", controllers.DeleteUser).Methods("DELETE")

}

0 comments on commit e11f69e

Please sign in to comment.