feat: add service and model

This commit is contained in:
Jeffrey Duroyon
2020-05-10 23:58:49 +02:00
parent 4369438ff2
commit c8993f1ca3
20 changed files with 89 additions and 29 deletions

View File

@@ -2,22 +2,22 @@ package handlers
import "github.com/gin-gonic/gin"
func (hc *handlersContext) getAllCages(c *gin.Context){
func (hc *handlersContext) getAllCages(c *gin.Context) {
}
func (hc *handlersContext) getACage(c *gin.Context){
func (hc *handlersContext) getACage(c *gin.Context) {
}
func (hc *handlersContext) createACage(c *gin.Context){
func (hc *handlersContext) createACage(c *gin.Context) {
}
func (hc *handlersContext) updateACage(c *gin.Context){
func (hc *handlersContext) updateACage(c *gin.Context) {
}
func (hc *handlersContext) deleteACage(c *gin.Context){
func (hc *handlersContext) deleteACage(c *gin.Context) {
}

View File

@@ -2,26 +2,26 @@ package handlers
import "github.com/gin-gonic/gin"
func (hc *handlersContext) getAllGames(c *gin.Context){
func (hc *handlersContext) getAllGames(c *gin.Context) {
}
func (hc *handlersContext) getAGame(c *gin.Context){
func (hc *handlersContext) getAGame(c *gin.Context) {
}
func (hc *handlersContext) createAGame(c *gin.Context){
func (hc *handlersContext) createAGame(c *gin.Context) {
}
func (hc *handlersContext) updateAGame(c *gin.Context){
func (hc *handlersContext) updateAGame(c *gin.Context) {
}
func (hc *handlersContext) deleteAGame(c *gin.Context){
func (hc *handlersContext) deleteAGame(c *gin.Context) {
}
func (hc *handlersContext) getHamstersOfGame(c *gin.Context){
func (hc *handlersContext) getHamstersOfGame(c *gin.Context) {
}
}

View File

@@ -2,22 +2,22 @@ package handlers
import "github.com/gin-gonic/gin"
func (hc *handlersContext) getAllHamsters(c *gin.Context){
func (hc *handlersContext) getAllHamsters(c *gin.Context) {
}
func (hc *handlersContext) getAHamster(c *gin.Context){
func (hc *handlersContext) getAHamster(c *gin.Context) {
}
func (hc *handlersContext) createAHamster(c *gin.Context){
func (hc *handlersContext) createAHamster(c *gin.Context) {
}
func (hc *handlersContext) updateAHamster(c *gin.Context){
func (hc *handlersContext) updateAHamster(c *gin.Context) {
}
func (hc *handlersContext) deleteAHamster(c *gin.Context){
func (hc *handlersContext) deleteAHamster(c *gin.Context) {
}

View File

@@ -65,7 +65,7 @@ func NewRouter(config *Config) *gin.Engine {
userRoute.Handle("LOGIN", "", hc.connectUser)
userRoute.Handle(http.MethodPost, "", hc.createUser)
securedUserRoute := userRoute.Group("")
securedUserRoute := userRoute.Group("")
//TODO add secure auth
securedUserRoute.Handle(http.MethodGet, "", hc.getAllUsers)
securedUserRoute.Handle(http.MethodGet, "/:userId", hc.getUser)
@@ -77,19 +77,19 @@ func NewRouter(config *Config) *gin.Engine {
gameRoute.Handle(http.MethodGet, "", hc.getAllGames)
gameRoute.Handle(http.MethodGet, "/:gameId", hc.getAGame)
gameRoute.Handle(http.MethodPost, "/:gameId", hc.createAGame)
gameRoute.Handle(http.MethodPut, "/:gameId",hc.updateAGame)
gameRoute.Handle(http.MethodGet, "/:gameId/hamsters",hc.getHamstersOfGame)
gameRoute.Handle(http.MethodDelete, "/:gameId",hc.deleteAGame)
gameRoute.Handle(http.MethodPut, "/:gameId", hc.updateAGame)
gameRoute.Handle(http.MethodGet, "/:gameId/hamsters", hc.getHamstersOfGame)
gameRoute.Handle(http.MethodDelete, "/:gameId", hc.deleteAGame)
cageRoute := gameRoute.Group("/:gameId/cages")
cageRoute.Handle(http.MethodGet, "",hc.getAllCages)
cageRoute.Handle(http.MethodGet, "", hc.getAllCages)
cageRoute.Handle(http.MethodGet, "/:cageId", hc.getACage)
cageRoute.Handle(http.MethodPost, "/:cageId", hc.createACage)
cageRoute.Handle(http.MethodPut, "/:cageId", hc.updateACage)
cageRoute.Handle(http.MethodDelete, "/:cageId",hc.deleteACage)
cageRoute.Handle(http.MethodDelete, "/:cageId", hc.deleteACage)
hamsterRoute := cageRoute.Group("/:cageId/hamsters")
hamsterRoute.Handle(http.MethodGet, "",hc.getAllHamsters)
hamsterRoute.Handle(http.MethodGet, "", hc.getAllHamsters)
hamsterRoute.Handle(http.MethodGet, "/:hamsterId", hc.getAHamster)
hamsterRoute.Handle(http.MethodPost, "/:hamsterId", hc.createAHamster)
hamsterRoute.Handle(http.MethodPut, "/:hamsterId", hc.updateAHamster)

11
service/cage_service.go Normal file
View File

@@ -0,0 +1,11 @@
package service
import "hamster-tycoon/storage/dao"
type CageService struct {
serviceContext
}
func NewCageService(database dao.Database) *CageService {
return &CageService{serviceContext{db: database}}
}

11
service/game_service.go Normal file
View File

@@ -0,0 +1,11 @@
package service
import "Game-tycoon/storage/dao"
type GameService struct {
serviceContext
}
func NewGameService(database dao.Database) *GameService {
return &GameService{serviceContext{db: database}}
}

View File

@@ -0,0 +1,11 @@
package service
import "hamster-tycoon/storage/dao"
type HamsterService struct {
serviceContext
}
func NewHamsterService(database dao.Database) *HamsterService {
return &HamsterService{serviceContext{db: database}}
}

View File

@@ -15,4 +15,12 @@ type Database interface {
UpdateUser(*model.User) error
// end: user dao funcs
// start: cage games funcs
GetAllGames() ([]*model.Game, error)
GetGameById(string) (*model.Game, error)
CreateGame(*model.Game) error
DeleteGame(string) error
UpdateGame(*model.Game) error
// end: games dao funcs
}

View File

@@ -0,0 +1 @@
package fake

View File

@@ -0,0 +1 @@
package fake

View File

@@ -0,0 +1 @@
package fake

View File

@@ -0,0 +1 @@
package postgresql

View File

@@ -0,0 +1 @@
package postgresql

View File

@@ -0,0 +1 @@
package postgresql

View File

@@ -1,4 +1,4 @@
package hamster_tycoon
package model
type Cage struct {
Hamsters []*Hamster

View File

@@ -1,6 +1,8 @@
package hamster_tycoon
package model
type Game struct {
ID string
server Server
Cages []*Cage
SelledHamster []*Hamster
}

View File

@@ -1,8 +1,9 @@
package hamster_tycoon
package model
import (
"errors"
"fmt"
uuid "github.com/satori/go.uuid"
"hamster-tycoon/randomizer"
)
@@ -29,6 +30,7 @@ const (
var GlobalHamsterNumber = 1
type Hamster struct {
ID string
Name string
Number int
Sexe string
@@ -113,6 +115,7 @@ func Born(father *Hamster, mother *Hamster) ([]*Hamster, error) {
child := make([]*Hamster, numberOfChild)
for i := 0; i < numberOfChild; i++ {
child[i] = &Hamster{
ID: uuid.NewV4().String(),
Name: fmt.Sprintf("Hamster %d", GlobalHamsterNumber),
Number: GlobalHamsterNumber,
Age: 1,

View File

@@ -1,4 +1,4 @@
package hamster_tycoon
package model
import (
"errors"

5
storage/model/server.go Normal file
View File

@@ -0,0 +1,5 @@
package model
type Server struct {
ID string
}

View File

@@ -1,6 +1,8 @@
package model
import "time"
import (
"time"
)
type User struct {
UserEditable
@@ -8,6 +10,7 @@ type User struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
GoogleId string `json:"-"`
Games []Game
}
type UserEditable struct {