This required service and repo creation for the users. Logging out is not yet supported, but that is next.
36 lines
974 B
Go
36 lines
974 B
Go
package service
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/user"
|
|
)
|
|
|
|
// UserService implements the domain.UserService defined in the domain module.
|
|
type UserService struct {
|
|
userRepository domain.UserRepository
|
|
}
|
|
|
|
// Compile-time check to ensure the UserService implements domain.UserService
|
|
var _ domain.UserService = (*UserService)(nil)
|
|
|
|
// NewUserService creates a user service object which can be passed into the context. The service
|
|
// requires a user repository which it will use to hit the database when needed.
|
|
func NewUserService(userRepository domain.UserRepository) domain.UserService {
|
|
return &UserService{userRepository: userRepository}
|
|
}
|
|
|
|
func (s *UserService) GetAuthenicatedUser(ctx *gin.Context) domain.User {
|
|
val, ok := ctx.Get("userId")
|
|
if !ok {
|
|
return domain.User{}
|
|
}
|
|
|
|
id := val.(int)
|
|
user, err := s.userRepository.GetUserById(id)
|
|
if err != nil {
|
|
return domain.User{}
|
|
}
|
|
|
|
return *user
|
|
}
|