Add login
This commit is contained in:
7
service/service.go
Normal file
7
service/service.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package service
|
||||
|
||||
import "hamster-tycoon/storage/dao"
|
||||
|
||||
type serviceContext struct {
|
||||
db dao.Database
|
||||
}
|
||||
39
service/user_service.go
Normal file
39
service/user_service.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"hamster-tycoon/storage/dao"
|
||||
"hamster-tycoon/storage/model"
|
||||
"hamster-tycoon/utils"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
serviceContext
|
||||
}
|
||||
|
||||
func NewUserService(database dao.Database) *UserService {
|
||||
return &UserService{serviceContext{db: database}}
|
||||
}
|
||||
|
||||
func (us *UserService) GetUserFromGoogleId(googleUserId string) (*model.User, error) {
|
||||
user, err := us.db.GetUsersByGoogleID(googleUserId)
|
||||
if err != nil {
|
||||
if castedError, ok := err.(*dao.DAOError); ok {
|
||||
switch castedError.Type {
|
||||
case dao.ErrTypeNotFound:
|
||||
return nil, &model.ErrNotFound
|
||||
default:
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
}
|
||||
return nil, &model.ErrInternalServer
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (us *UserService) CreateUserFromGoogleToken(id string, email string) (*model.User, error) {
|
||||
randNick := utils.String(20)
|
||||
user := &model.User{UserEditable: model.UserEditable{Email: email, Nickname: "player-" + randNick}, GoogleId: id}
|
||||
err := us.db.CreateUser(user)
|
||||
return user, err
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user