From ee7790dd39b64293f23a9f027529e17400fcbf3d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 26 Nov 2024 15:08:31 -0800 Subject: [PATCH] delete everything that won't compile --- internal/controller/submissions.go | 100 ------------------------ internal/controller/users.go | 108 -------------------------- server.go | 117 ----------------------------- 3 files changed, 325 deletions(-) diff --git a/internal/controller/submissions.go b/internal/controller/submissions.go index 461e894..6a52386 100644 --- a/internal/controller/submissions.go +++ b/internal/controller/submissions.go @@ -2,12 +2,8 @@ package controller import ( "context" - "fmt" "git.itzana.me/strafesnet/maps-service/internal/datastore" - "git.itzana.me/strafesnet/maps-service/internal/model" "git.itzana.me/strafesnet/maps-service/api" - "github.com/pkg/errors" - "time" ) type Submissions struct { @@ -35,99 +31,3 @@ func (m Submissions) Get(ctx context.Context, params *api.GetSubmissionParams) ( StatusID: api.NewOptInt32(item.StatusID), }, nil } - -func (m Submissions) GetList(ctx context.Context, list *maps.IdList) (*maps.MapList, error) { - items, err := m.Store.Maps().GetList(ctx, list.ID) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to get maps").Error()) - } - - var resp maps.MapList - for i := 0; i < len(items); i++ { - resp.Maps = append(resp.Maps, &maps.MapResponse{ - ID: items[i].ID, - DisplayName: items[i].DisplayName, - Creator: items[i].Creator, - GameID: items[i].GameID, - Date: items[i].Date.Unix(), - }) - } - - return &resp, nil -} - -func (m Submissions) Update(ctx context.Context, request *maps.MapRequest) (*maps.NullResponse, error) { - updates := datastore.Optional() - updates.AddNotNil("display_name", request.DisplayName) - updates.AddNotNil("creator", request.Creator) - updates.AddNotNil("game_id", request.GameID) - if request.Date != nil { - updates.AddNotNil("date", time.Unix(request.GetDate(), 0)) - } - - if err := m.Store.Maps().Update(ctx, request.GetID(), updates); err != nil { - if err == datastore.ErrNotExist { - return nil, status.Error(codes.NotFound, "map does not exit") - } - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to update map:").Error()) - } - - return &maps.NullResponse{}, nil -} - -func (m Submissions) Create(ctx context.Context, request *maps.MapRequest) (*maps.IdMessage, error) { - item, err := m.Store.Maps().Create(ctx, model.Submission{ - ID: request.GetID(), - DisplayName: request.GetDisplayName(), - Creator: request.GetCreator(), - GameID: request.GetGameID(), - Date: time.Unix(request.GetDate(), 0), - }) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to create map:").Error()) - } - - return &maps.IdMessage{ID: item.ID}, nil -} - -func (m Submissions) Delete(ctx context.Context, message *maps.IdMessage) (*maps.NullResponse, error) { - if err := m.Store.Maps().Delete(ctx, message.GetID()); err != nil { - if err == datastore.ErrNotExist { - return nil, status.Error(codes.NotFound, "map does not exit") - } - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to delete map:").Error()) - } - - return &maps.NullResponse{}, nil -} - -func (m Submissions) List(ctx context.Context, request *maps.ListRequest) (*maps.MapList, error) { - filter := datastore.Optional() - fmt.Println(request) - if request.Filter != nil { - filter.AddNotNil("display_name", request.GetFilter().DisplayName) - filter.AddNotNil("creator", request.GetFilter().Creator) - filter.AddNotNil("game_id", request.GetFilter().GameID) - } - - items, err := m.Store.Maps().List(ctx, filter, model.Page{ - Number: request.GetPage().GetNumber(), - Size: request.GetPage().GetSize(), - }) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to get maps:").Error()) - } - - var resp maps.MapList - for i := 0; i < len(items); i++ { - resp.Maps = append(resp.Maps, &maps.MapResponse{ - ID: items[i].ID, - DisplayName: items[i].DisplayName, - Creator: items[i].Creator, - GameID: items[i].GameID, - Date: items[i].Date.Unix(), - }) - } - - return &resp, nil -} diff --git a/internal/controller/users.go b/internal/controller/users.go index 2315636..00fd8d2 100644 --- a/internal/controller/users.go +++ b/internal/controller/users.go @@ -1,117 +1,9 @@ package controller import ( - "context" "git.itzana.me/strafesnet/maps-service/internal/datastore" - "git.itzana.me/strafesnet/maps-service/internal/model" - "git.itzana.me/strafesnet/go-grpc/users" - "github.com/pkg/errors" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type Users struct { - *users.UnimplementedUsersServiceServer Store datastore.Datastore } - -func (u Users) Get(ctx context.Context, request *users.IdMessage) (*users.UserResponse, error) { - ur, err := u.Store.Users().Get(ctx, request.ID) - if err != nil { - if err == datastore.ErrNotExist { - return nil, status.Error(codes.NotFound, err.Error()) - } - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to get user").Error()) - } - - return &users.UserResponse{ - ID: ur.ID, - Username: ur.Username, - StateID: ur.StateID, - }, nil -} - -func (u Users) GetList(ctx context.Context, list *users.IdList) (*users.UserList, error) { - uList, err := u.Store.Users().GetList(ctx, list.ID) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to get users").Error()) - } - - var resp users.UserList - for i := 0; i < len(uList); i++ { - resp.Users = append(resp.Users, &users.UserResponse{ - ID: uList[i].ID, - Username: uList[i].Username, - StateID: uList[i].StateID, - }) - } - - return &resp, nil -} - -func (u Users) Update(ctx context.Context, request *users.UserRequest) (*users.NullResponse, error) { - updates := datastore.Optional() - updates.AddNotNil("state_id", request.StateID) - updates.AddNotNil("username", request.Username) - - if err := u.Store.Users().Update(ctx, request.GetID(), updates); err != nil { - if err == datastore.ErrNotExist { - return nil, status.Error(codes.NotFound, err.Error()) - } - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to update user").Error()) - } - - return &users.NullResponse{}, nil -} - -func (u Users) Create(ctx context.Context, request *users.UserRequest) (*users.IdMessage, error) { - us, err := u.Store.Users().Create(ctx, model.User{ - ID: request.GetID(), - Username: request.GetUsername(), - StateID: request.GetStateID(), - }) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to create user").Error()) - } - - return &users.IdMessage{ID: us.ID}, nil -} - -func (u Users) Delete(ctx context.Context, request *users.IdMessage) (*users.NullResponse, error) { - if err := u.Store.Users().Delete(ctx, request.GetID()); err != nil { - if err == datastore.ErrNotExist { - return nil, status.Error(codes.NotFound, err.Error()) - } - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to delete user").Error()) - } - - return &users.NullResponse{}, nil -} - -func (u Users) List(ctx context.Context, request *users.ListRequest) (*users.UserList, error) { - filters := datastore.Optional() - if request.Filter != nil { - filters.AddNotNil("id", request.GetFilter().ID) - filters.AddNotNil("state_id", request.GetFilter().StateID) - filters.AddNotNil("username", request.GetFilter().Username) - } - - uList, err := u.Store.Users().List(ctx, filters, model.Page{ - Number: request.GetPage().GetNumber(), - Size: request.GetPage().GetSize(), - }) - if err != nil { - return nil, status.Error(codes.Internal, errors.Wrap(err, "failed to get filtered users").Error()) - } - - var uResp users.UserList - for i := 0; i < len(uList); i++ { - uResp.Users = append(uResp.Users, &users.UserResponse{ - ID: uList[i].ID, - Username: uList[i].Username, - StateID: uList[i].StateID, - }) - } - - return &uResp, nil -} diff --git a/server.go b/server.go index dd2dd5b..5775e17 100644 --- a/server.go +++ b/server.go @@ -28,123 +28,6 @@ func (m *apiServer) NewError(ctx context.Context, err error) *api.ErrorStatusCod } } -// GetTimes implements api.Handler. -func (m *apiServer) ListTimes(ctx context.Context, params api.ListTimesParams) ([]api.Time, error) { - client := times.NewTimesServiceClient(m.client) - - // Call the List method using params - req := ×.ListRequest{ - Page: ×.Pagination{ - Number: params.Page.GetPage(), - Size: params.Page.GetLimit(), - }, - } - - if filter, ok := params.Filter.Get(); ok { - if id := filter.GetID(); id.IsSet() { - req.Filter.ID = &id.Value - } - if time := filter.GetTime(); time.IsSet() { - req.Filter.Time = &time.Value - } - if userID := filter.GetUserID(); userID.IsSet() { - req.Filter.UserID = &userID.Value - } - if mapID := filter.GetMapID(); mapID.IsSet() { - req.Filter.MapID = &mapID.Value - } - if styleID := filter.GetStyleID(); styleID.IsSet() { - req.Filter.StyleID = &styleID.Value - } - if modeID := filter.GetModeID(); modeID.IsSet() { - req.Filter.ModeID = &modeID.Value - } - if gameID := filter.GetGameID(); gameID.IsSet() { - req.Filter.GameID = &gameID.Value - } - } - - response, err := client.List(ctx, req) - if err != nil { - return nil, err - } - - return convertTimes(response.Times), nil -} - -// GetUser implements api.Handler. -func (m *apiServer) GetUser(ctx context.Context, params api.GetUserParams) (*api.User, error) { - client := users.NewUsersServiceClient(m.client) - - response, err := client.Get(ctx, &users.IdMessage{ - ID: params.UserID, - }) - if err != nil { - return nil, err - } - - return &api.User{ - ID: api.NewOptInt64(response.ID), - Username: api.NewOptString(response.Username), - StateID: api.NewOptInt32(response.StateID), - }, nil -} - -// ListRanks implements api.Handler. -func (m *apiServer) ListRanks(ctx context.Context, params api.ListRanksParams) ([]api.Rank, error) { - client := ranks.NewRanksServiceClient(m.client) - - req := &ranks.ListRequest{ - Page: &ranks.Pagination{ - Number: params.Page.GetPage(), - Size: params.Page.GetLimit(), - }, - } - - if filter, ok := params.Filter.Get(); ok { - if gameID, ok := filter.GetGameID().Get(); ok { - req.GameID = gameID - } - - if modeID, ok := filter.GetModeID().Get(); ok { - req.ModeID = modeID - } - - if styleID, ok := filter.GetStyleID().Get(); ok { - req.StyleID = styleID - } - - if sort, ok := filter.GetSort().Get(); ok { - req.Sort = sort - } - } - - response, err := client.List(ctx, req) - if err != nil { - return nil, err - } - - ranks := make([]api.Rank, len(response.Ranks)) - for i, r := range response.Ranks { - ranks[i] = api.Rank{ - ID: api.NewOptInt64(r.ID), - User: api.NewOptUser(api.User{ - ID: api.NewOptInt64(r.User.ID), - Username: api.NewOptString(r.User.Username), - StateID: api.NewOptInt32(r.User.StateID), - }), - StyleID: api.NewOptInt32(r.StyleID), - ModeID: api.NewOptInt32(r.ModeID), - GameID: api.NewOptInt32(r.GameID), - Rank: api.NewOptFloat64(r.Rank), - Skill: api.NewOptFloat64(r.Skill), - UpdatedAt: api.NewOptInt64(r.UpdatedAt), - } - } - - return ranks, nil -} - func main() { // new grpc client conn, err := grpc.Dial("localhost:9000", grpc.WithTransportCredentials(insecure.NewCredentials()))