hackathon/api/user.go

157 lines
4.4 KiB
Go

2021-09-30 10:32:18 +07:00
package api
import (
2021-12-19 15:30:21 +07:00
"context"
2021-09-30 10:32:18 +07:00
"log"
2022-04-28 23:31:09 +07:00
"time"
2021-09-30 10:32:18 +07:00
2021-12-19 15:30:21 +07:00
codequestpb "github.com/hhhapz/codequest/api/v1"
"github.com/hhhapz/codequest/models"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2021-09-30 10:32:18 +07:00
)
type UserService struct {
2022-04-28 23:31:09 +07:00
codequestpb.UnsafeUserServiceServer
2021-12-19 15:30:21 +07:00
2021-09-30 10:32:18 +07:00
userStore UserStore
}
2022-04-28 23:31:09 +07:00
func (us *UserService) Info(ctx context.Context, req *codequestpb.InfoRequest) (*codequestpb.Info, error) {
u := User(ctx)
points, err := us.userStore.UserPoints(ctx, u)
if err != nil {
log.Printf("Could not retrieve user points(%q): %v", u.ID, err)
// no return
}
return &codequestpb.Info{
CurrentUser: convertUser(u),
Active: active(),
Points: int32(points),
StartTime: Start.UTC().Format(time.RFC3339),
EndTime: End.UTC().Format(time.RFC3339),
}, nil
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
func (us *UserService) UserByEmail(ctx context.Context, req *codequestpb.UserByEmailRequest) (*codequestpb.User, error) {
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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)
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
users, err := us.userStore.Users(ctx)
2021-09-30 10:32:18 +07:00
if err != nil {
2021-12-19 15:30:21 +07:00
log.Printf("Could not fetch all users: %v", err)
return nil, status.Errorf(codes.Internal, "unable to fetch users")
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
var userList []*codequestpb.User
for _, u := range users {
userList = append(userList, convertUser(u))
}
return &codequestpb.AllUsersResponse{Users: userList}, nil
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
func (us *UserService) UpdateUser(ctx context.Context, req *codequestpb.UpdateUserRequest) (*codequestpb.User, error) {
2022-04-28 23:31:09 +07:00
u := User(ctx)
2021-09-30 10:32:18 +07:00
2021-12-19 15:30:21 +07:00
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")
2021-09-30 10:32:18 +07:00
}
2021-12-22 14:38:22 +07:00
// TODO FIX THIS
// 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")
// }
2021-09-30 10:32:18 +07:00
2021-12-19 15:30:21 +07:00
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")
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
return convertUser(u), nil
2021-09-30 10:32:18 +07:00
}
2022-04-28 23:31:09 +07:00
func (us *UserService) AdminUpdateUser(ctx context.Context, req *codequestpb.AdminUpdateUserRequest) (*codequestpb.User, error) {
2021-12-19 15:30:21 +07:00
var err error
if ctx, err = AdminOnly(ctx); err != nil {
return nil, err
2021-09-30 10:32:18 +07:00
}
2022-04-28 23:31:09 +07:00
user, err := us.userStore.User(ctx, req.Body.Email)
2021-12-19 15:30:21 +07:00
if err != nil {
2022-04-28 23:31:09 +07:00
log.Printf("Could not fetch user(%q): %v", req.Body.Email, err)
return nil, status.Errorf(codes.NotFound, "email not found: %q", req.Body.Email)
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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")
2021-09-30 10:32:18 +07:00
}
2021-12-22 14:38:22 +07:00
// 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")
// }
2021-09-30 10:32:18 +07:00
2021-12-19 15:30:21 +07:00
user.Admin = req.Body.Admin
2021-09-30 10:32:18 +07:00
2021-12-19 15:30:21 +07:00
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")
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
return convertUser(user), nil
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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)
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
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")
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
return convertUser(user), nil
2021-09-30 10:32:18 +07:00
}
2021-12-19 15:30:21 +07:00
func convertUser(u *models.User) *codequestpb.User {
return &codequestpb.User{
2021-12-22 14:38:22 +07:00
ID: u.ID,
Name: u.Name,
Email: u.Email,
Picture: u.Picture,
Admin: u.Admin,
2022-04-28 23:31:09 +07:00
CreatedAt: u.CreatedAt.Time().UTC().Format(time.RFC3339),
2021-12-19 15:30:21 +07:00
}
2021-09-30 10:32:18 +07:00
}