hackathon/api/user.go

151 lines
4.2 KiB
Go

package api
import (
"context"
"database/sql"
"log"
codequestpb "github.com/hhhapz/codequest/api/v1"
"github.com/hhhapz/codequest/models"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
type UserService struct {
codequestpb.UnimplementedUserServiceServer
userStore UserStore
}
func (us *UserService) User(ctx context.Context, req *codequestpb.UserRequest) (*codequestpb.User, error) {
u := UserCtx(ctx)
return convertUser(u), nil
}
func (us *UserService) UserByEmail(ctx context.Context, req *codequestpb.UserByEmailRequest) (*codequestpb.User, error) {
u := UserCtx(ctx)
if u.Email == req.Email {
return convertUser(u), nil
}
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
}
user, err := us.userStore.User(ctx, req.Email)
if err != nil {
log.Printf("Could not fetch user(%q): %v", req.Email, err)
return nil, status.Errorf(codes.NotFound, "email not found: %q", req.Email)
}
return convertUser(user), nil
}
func (us *UserService) AllUsers(ctx context.Context, req *codequestpb.AllUsersRequest) (*codequestpb.AllUsersResponse, error) {
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
}
users, err := us.userStore.Users(ctx)
if err != nil {
log.Printf("Could not fetch all users: %v", err)
return nil, status.Errorf(codes.Internal, "unable to fetch users")
}
var userList []*codequestpb.User
for _, u := range users {
userList = append(userList, convertUser(u))
}
return &codequestpb.AllUsersResponse{Users: userList}, nil
}
func (us *UserService) UpdateUser(ctx context.Context, req *codequestpb.UpdateUserRequest) (*codequestpb.User, error) {
u := UserCtx(ctx)
u.Name = req.Body.Name
if len(u.Name) < 3 || len(u.Name) > 20 {
return nil, status.Errorf(codes.InvalidArgument, "name must be between 3 and 20 characters")
}
gl := req.Body.GradeLevel
u.GradeLevel = sql.NullInt64{Int64: int64(gl), Valid: true}
if gl < 9 || gl > 12 {
return nil, status.Errorf(codes.InvalidArgument, "grade level must be between 9 and 12")
}
if err := us.userStore.UpdateUser(ctx, u); err != nil {
log.Printf("could not update user: %v", err)
return nil, status.Errorf(codes.Internal, "unable to update user")
}
return convertUser(u), nil
}
func (us *UserService) AdminUpdateUser(ctx context.Context, req *codequestpb.UpdateUserRequest) (*codequestpb.User, error) {
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
}
user, err := us.userStore.User(ctx, req.Email)
if err != nil {
log.Printf("Could not fetch user(%q): %v", req.Email, err)
return nil, status.Errorf(codes.NotFound, "email not found: %q", req.Email)
}
user.Name = req.Body.Name
if len(user.Name) < 3 || len(user.Name) > 20 {
return nil, status.Errorf(codes.InvalidArgument, "name must be between 3 and 20 characters")
}
gl := req.Body.GradeLevel
user.GradeLevel = sql.NullInt64{Int64: int64(gl), Valid: true}
if gl < 9 || gl > 12 {
return nil, status.Errorf(codes.InvalidArgument, "grade level must be between 9 and 12")
}
user.Admin = req.Body.Admin
if err := us.userStore.UpdateUser(ctx, user); err != nil {
log.Printf("could not update user: %v", err)
return nil, status.Errorf(codes.Internal, "unable to update user")
}
return convertUser(user), nil
}
func (us *UserService) DeleteUser(ctx context.Context, req *codequestpb.DeleteUserRequest) (*codequestpb.User, error) {
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
}
user, err := us.userStore.User(ctx, req.Email)
if err != nil {
log.Printf("Could not fetch user(%q): %v", req.Email, err)
return nil, status.Errorf(codes.NotFound, "email not found: %q", req.Email)
}
if err := us.userStore.DeleteUser(ctx, user); err != nil {
log.Printf("could not delete user: %v", err)
return nil, status.Errorf(codes.Internal, "unable to delete user")
}
return convertUser(user), nil
}
func convertUser(u *models.User) *codequestpb.User {
return &codequestpb.User{
Admin: u.Admin,
Email: u.Email,
GradeLevel: int32(u.GradeLevel.Int64),
ID: u.ID,
Name: u.Name,
Picture: u.Picture,
CreatedAt: timestamppb.New(u.CreatedAt.Time()),
}
}