MCPcopy Index your code
hub / github.com/dreamsofcode-io/grpc / Create

Method Create

pokemon-api/pokedex/server.go:27–65  ·  view source on GitHub ↗
(
	ctx context.Context,
	req *pb.PokemonRequest,
)

Source from the content-addressed store, hash-verified

25}
26
27func (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
67func (s *Server) Read(
68 ctx context.Context,

Callers

nothing calls this directly

Calls 3

pokemonToResponseFunction · 0.85
InsertMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected