( ctx context.Context, req *pb.PokemonRequest, )
| 25 | } |
| 26 | |
| 27 | func (s *Server) Create( |
| 28 | ctx context.Context, |
| 29 | req *pb.PokemonRequest, |
| 30 | ) (*pb.Pokemon, error) { |
| 31 | if req.Id == 0 { |
| 32 | return nil, status.Errorf(codes.InvalidArgument, "id cannot be 0") |
| 33 | } |
| 34 | |
| 35 | if req.Name == "" { |
| 36 | return nil, status.Errorf(codes.InvalidArgument, "name cannot be empty") |
| 37 | } |
| 38 | |
| 39 | if len(req.Type) == 0 { |
| 40 | return nil, status.Errorf(codes.InvalidArgument, "type cannot be empty") |
| 41 | } |
| 42 | |
| 43 | types := make([]string, len(req.Type)) |
| 44 | for i, t := range req.Type { |
| 45 | types[i] = t.String() |
| 46 | } |
| 47 | |
| 48 | now := time.Now() |
| 49 | |
| 50 | pokemon := Pokemon{ |
| 51 | ID: req.Id, |
| 52 | Name: req.Name, |
| 53 | Types: types, |
| 54 | CreatedAt: now, |
| 55 | UpdatedAt: now, |
| 56 | } |
| 57 | |
| 58 | if err := s.repository.Insert(ctx, pokemon); err != nil { |
| 59 | return nil, fmt.Errorf("failed to insert pokemon: %w", err) |
| 60 | } |
| 61 | |
| 62 | res := pokemonToResponse(pokemon) |
| 63 | |
| 64 | return &res, nil |
| 65 | } |
| 66 | |
| 67 | func (s *Server) Read( |
| 68 | ctx context.Context, |
nothing calls this directly
no test coverage detected