|
| 1 | +package grpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "dkhalife.com/tasks/core/internal/models" |
| 8 | + "dkhalife.com/tasks/core/internal/services/labels" |
| 9 | + "google.golang.org/grpc/codes" |
| 10 | + "google.golang.org/grpc/status" |
| 11 | +) |
| 12 | + |
| 13 | +type LabelGRPCServer struct { |
| 14 | + UnimplementedLabelServiceServer |
| 15 | + labelService *labels.LabelService |
| 16 | + getUserID func(ctx context.Context) (int, error) |
| 17 | +} |
| 18 | + |
| 19 | +func NewLabelGRPCServer(labelService *labels.LabelService, getUserID func(ctx context.Context) (int, error)) *LabelGRPCServer { |
| 20 | + return &LabelGRPCServer{ |
| 21 | + labelService: labelService, |
| 22 | + getUserID: getUserID, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (s *LabelGRPCServer) GetLabels(ctx context.Context, req *Empty) (*LabelsResponse, error) { |
| 27 | + userID, err := s.getUserID(ctx) |
| 28 | + if err != nil { |
| 29 | + return nil, status.Error(codes.Unauthenticated, "failed to get user identity") |
| 30 | + } |
| 31 | + |
| 32 | + statusCode, result := s.labelService.GetUserLabels(ctx, userID) |
| 33 | + if statusCode != http.StatusOK { |
| 34 | + return nil, status.Error(codes.Internal, "failed to get labels") |
| 35 | + } |
| 36 | + |
| 37 | + data, ok := result.(map[string]interface{}) |
| 38 | + if !ok { |
| 39 | + return nil, status.Error(codes.Internal, "unexpected response format") |
| 40 | + } |
| 41 | + |
| 42 | + labelResponses, ok := data["labels"].([]map[string]interface{}) |
| 43 | + if !ok { |
| 44 | + return nil, status.Error(codes.Internal, "unexpected labels format") |
| 45 | + } |
| 46 | + |
| 47 | + grpcLabels := make([]*Label, len(labelResponses)) |
| 48 | + for i, l := range labelResponses { |
| 49 | + id, _ := l["id"].(int) |
| 50 | + name, _ := l["name"].(string) |
| 51 | + color, _ := l["color"].(string) |
| 52 | + grpcLabels[i] = &Label{ |
| 53 | + Id: int32(id), |
| 54 | + Name: name, |
| 55 | + Color: color, |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return &LabelsResponse{Labels: grpcLabels}, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (s *LabelGRPCServer) CreateLabel(ctx context.Context, req *CreateLabelRequest) (*LabelResponse, error) { |
| 63 | + userID, err := s.getUserID(ctx) |
| 64 | + if err != nil { |
| 65 | + return nil, status.Error(codes.Unauthenticated, "failed to get user identity") |
| 66 | + } |
| 67 | + |
| 68 | + createReq := models.CreateLabelReq{ |
| 69 | + Name: req.GetName(), |
| 70 | + Color: req.GetColor(), |
| 71 | + } |
| 72 | + |
| 73 | + statusCode, result := s.labelService.CreateLabel(ctx, userID, createReq) |
| 74 | + if statusCode != http.StatusCreated { |
| 75 | + return nil, status.Error(codes.Internal, "failed to create label") |
| 76 | + } |
| 77 | + |
| 78 | + data, ok := result.(map[string]interface{}) |
| 79 | + if !ok { |
| 80 | + return nil, status.Error(codes.Internal, "unexpected response format") |
| 81 | + } |
| 82 | + |
| 83 | + labelData, ok := data["label"].(map[string]interface{}) |
| 84 | + if !ok { |
| 85 | + return nil, status.Error(codes.Internal, "unexpected label format") |
| 86 | + } |
| 87 | + |
| 88 | + id, _ := labelData["id"].(int) |
| 89 | + name, _ := labelData["name"].(string) |
| 90 | + color, _ := labelData["color"].(string) |
| 91 | + |
| 92 | + return &LabelResponse{ |
| 93 | + Label: &Label{ |
| 94 | + Id: int32(id), |
| 95 | + Name: name, |
| 96 | + Color: color, |
| 97 | + }, |
| 98 | + }, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (s *LabelGRPCServer) UpdateLabel(ctx context.Context, req *UpdateLabelRequest) (*LabelResponse, error) { |
| 102 | + userID, err := s.getUserID(ctx) |
| 103 | + if err != nil { |
| 104 | + return nil, status.Error(codes.Unauthenticated, "failed to get user identity") |
| 105 | + } |
| 106 | + |
| 107 | + updateReq := models.UpdateLabelReq{ |
| 108 | + ID: int(req.GetId()), |
| 109 | + CreateLabelReq: models.CreateLabelReq{ |
| 110 | + Name: req.GetName(), |
| 111 | + Color: req.GetColor(), |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + statusCode, result := s.labelService.UpdateLabel(ctx, userID, updateReq) |
| 116 | + if statusCode == http.StatusForbidden { |
| 117 | + return nil, status.Error(codes.PermissionDenied, "you are not allowed to perform this update") |
| 118 | + } |
| 119 | + if statusCode != http.StatusOK { |
| 120 | + return nil, status.Error(codes.Internal, "failed to update label") |
| 121 | + } |
| 122 | + |
| 123 | + data, ok := result.(map[string]interface{}) |
| 124 | + if !ok { |
| 125 | + return nil, status.Error(codes.Internal, "unexpected response format") |
| 126 | + } |
| 127 | + |
| 128 | + labelData, ok := data["label"].(map[string]interface{}) |
| 129 | + if !ok { |
| 130 | + return nil, status.Error(codes.Internal, "unexpected label format") |
| 131 | + } |
| 132 | + |
| 133 | + id, _ := labelData["id"].(int) |
| 134 | + name, _ := labelData["name"].(string) |
| 135 | + color, _ := labelData["color"].(string) |
| 136 | + |
| 137 | + return &LabelResponse{ |
| 138 | + Label: &Label{ |
| 139 | + Id: int32(id), |
| 140 | + Name: name, |
| 141 | + Color: color, |
| 142 | + }, |
| 143 | + }, nil |
| 144 | +} |
| 145 | + |
| 146 | +func (s *LabelGRPCServer) DeleteLabel(ctx context.Context, req *DeleteLabelRequest) (*Empty, error) { |
| 147 | + userID, err := s.getUserID(ctx) |
| 148 | + if err != nil { |
| 149 | + return nil, status.Error(codes.Unauthenticated, "failed to get user identity") |
| 150 | + } |
| 151 | + |
| 152 | + statusCode, _ := s.labelService.DeleteLabel(ctx, userID, int(req.GetId())) |
| 153 | + if statusCode != http.StatusNoContent { |
| 154 | + return nil, status.Error(codes.Internal, "failed to delete label") |
| 155 | + } |
| 156 | + |
| 157 | + return &Empty{}, nil |
| 158 | +} |
0 commit comments