-
Notifications
You must be signed in to change notification settings - Fork 0
[MR-1] для интервью #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package model | ||
|
|
||
| type Cat struct { | ||
| ID int64 `json:"id"` | ||
| Age int64 `json:"age"` | ||
| Name string `json:"name"` | ||
| Color string `json:"color"` | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "github.com/pkg/errors" | ||
| "github.com/rcv911/service_template/internal/model" | ||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| type Repository struct { | ||
| logger zerolog.Logger | ||
| storage map[int64]model.Cat | ||
| } | ||
|
|
||
| func New(logger zerolog.Logger) *Repository { | ||
| return &Repository{ | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (r *Repository) GetCat(id int64) (*model.Cat, error) { | ||
| cat, ok := r.storage[id] | ||
| if !ok { | ||
| return nil, errors.New("cat not found") | ||
| } | ||
|
|
||
| return &cat, nil | ||
| } | ||
|
|
||
| func (r *Repository) CreateCat(age int64, name, color string) error { | ||
| newCat := model.Cat{ | ||
| Age: age, | ||
| Name: name, | ||
| Color: color, | ||
| } | ||
|
|
||
| r.storage[newCat.ID] = newCat | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не хватает new id |
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (r *Repository) ListCats() []model.Cat { | ||
| var cats []model.Cat | ||
|
|
||
| for _, cat := range r.storage { | ||
| cats = append(cats, cat) | ||
| } | ||
|
|
||
| return cats | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/rcv911/service_template/pkg/cat_admin_v1" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func (s *CatAdminServiceServer) CreateCat(ctx context.Context, req *cat_admin_v1.CreateCatRequest) (*cat_admin_v1.CatResponse, error) { | ||
| err := s.repo.CreateCat(int64(req.GetAge()), req.GetName(), req.GetColor()) | ||
| if err != nil { | ||
| return nil, status.Error(codes.Internal, err.Error()) | ||
| } | ||
|
|
||
| return &cat_admin_v1.CatResponse{}, nil | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нарушение контракта |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/rcv911/service_template/pkg/cat_admin_v1" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // GetCat получение кота | ||
| func (s *CatAdminServiceServer) GetCat(ctx context.Context, req *cat_admin_v1.GetCatRequest) (*cat_admin_v1.CatResponse, error) { | ||
| cat, err := s.repo.GetCat(req.GetId()) | ||
| if err != nil { | ||
| return nil, status.Errorf(codes.Internal, "failed get cat: %v", err) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нужно - %w |
||
| } | ||
|
|
||
| return &cat_admin_v1.CatResponse{ | ||
| Id: cat.ID, | ||
| Name: cat.Name, | ||
| Age: int32(cat.Age), | ||
| Color: cat.Color, | ||
| }, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,16 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/rcv911/service_template/pkg/cat_admin_v1" | ||
| "github.com/rcv911/service_template/pkg/cat_v1" | ||
| "google.golang.org/grpc" | ||
| ) | ||
|
|
||
| type CatAdminServiceServer struct { | ||
| cat_admin_v1.UnimplementedCatAdminServiceServer | ||
| } | ||
|
|
||
| type CatServiceServer struct { | ||
| cat_v1.UnimplementedCatServiceServer | ||
| } | ||
|
|
||
| func NewGRPCSServer() *grpc.Server { | ||
| func NewGRPCSServer(catService *CatServiceServer, catAdminService *CatAdminServiceServer) *grpc.Server { | ||
| grpcServer := grpc.NewServer() | ||
|
|
||
| cat_admin_v1.RegisterCatAdminServiceServer(grpcServer, &CatAdminServiceServer{}) | ||
| cat_v1.RegisterCatServiceServer(grpcServer, &CatServiceServer{}) | ||
| cat_admin_v1.RegisterCatAdminServiceServer(grpcServer, catAdminService) | ||
| cat_v1.RegisterCatServiceServer(grpcServer, catService) | ||
|
|
||
| return grpcServer | ||
| } | ||
|
|
||
| // GetCat получение кота | ||
| func (s *CatAdminServiceServer) GetCat(ctx context.Context, req *cat_admin_v1.GetCatRequest) (*cat_admin_v1.CatResponse, error) { | ||
| return &cat_admin_v1.CatResponse{ | ||
| Id: req.Id, | ||
| Name: "Барсик", | ||
| Age: 3, | ||
| Color: "Черный", | ||
| }, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/rcv911/service_template/pkg/cat_admin_v1" | ||
| ) | ||
|
|
||
| func (s *CatAdminServiceServer) ListCats(ctx context.Context, req *cat_admin_v1.ListCatsRequest) (*cat_admin_v1.ListCatsResponse, error) { | ||
| cats := s.repo.ListCats() | ||
|
|
||
| var resp []*cat_admin_v1.CatResponse | ||
|
|
||
| for _, cat := range cats { | ||
| resp = append(resp, &cat_admin_v1.CatResponse{ | ||
| Id: cat.ID, | ||
| Name: cat.Name, | ||
| Age: int32(cat.Age), | ||
| Color: cat.Color, | ||
| }) | ||
| } | ||
|
|
||
| return &cat_admin_v1.ListCatsResponse{ | ||
| Cats: resp, | ||
| TotalCount: int32(len(cats)), | ||
| }, nil | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "github.com/rcv911/service_template/internal/model" | ||
| "github.com/rcv911/service_template/pkg/cat_admin_v1" | ||
| "github.com/rcv911/service_template/pkg/cat_v1" | ||
| ) | ||
|
|
||
| type Repository interface { | ||
| ListCats() []model.Cat | ||
| CreateCat(age int64, name, color string) error | ||
| GetCat(id int64) (*model.Cat, error) | ||
| } | ||
|
|
||
| type CatAdminServiceServer struct { | ||
| cat_admin_v1.UnimplementedCatAdminServiceServer | ||
| repo Repository | ||
| } | ||
|
|
||
| type CatServiceServer struct { | ||
| cat_v1.UnimplementedCatServiceServer | ||
| repo Repository | ||
| } | ||
|
|
||
| func NewCatServiceServer(repo Repository) *CatServiceServer { | ||
| return &CatServiceServer{repo: repo} | ||
| } | ||
|
|
||
| func NewCatAdminServiceServer(repo Repository) *CatAdminServiceServer { | ||
| return &CatAdminServiceServer{repo: repo} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
нужно l