|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/go-chi/chi/v5" |
5 |
| - "net/http" |
6 |
| - "scrumlr.io/server/users" |
7 |
| - |
8 |
| - "github.com/go-chi/render" |
9 |
| - "github.com/google/uuid" |
10 |
| - "go.opentelemetry.io/otel/codes" |
11 |
| - "scrumlr.io/server/common" |
12 |
| - "scrumlr.io/server/identifiers" |
13 |
| - "scrumlr.io/server/logger" |
14 |
| - "scrumlr.io/server/sessions" |
| 4 | + "github.com/go-chi/chi/v5" |
| 5 | + "net/http" |
| 6 | + "scrumlr.io/server/users" |
| 7 | + |
| 8 | + "github.com/go-chi/render" |
| 9 | + "github.com/google/uuid" |
| 10 | + "go.opentelemetry.io/otel/codes" |
| 11 | + "scrumlr.io/server/common" |
| 12 | + "scrumlr.io/server/identifiers" |
| 13 | + "scrumlr.io/server/logger" |
| 14 | + "scrumlr.io/server/sessions" |
15 | 15 | )
|
16 | 16 |
|
17 | 17 | //var tracer trace.Tracer = otel.Tracer("scrumlr.io/server/api")
|
18 | 18 |
|
19 | 19 | // getUser get a user
|
20 | 20 | func (s *Server) getUser(w http.ResponseWriter, r *http.Request) {
|
21 |
| - ctx, span := tracer.Start(r.Context(), "scrumlr.users.api.get") |
22 |
| - defer span.End() |
| 21 | + ctx, span := tracer.Start(r.Context(), "scrumlr.users.api.get") |
| 22 | + defer span.End() |
23 | 23 |
|
24 |
| - userId := ctx.Value(identifiers.UserIdentifier).(uuid.UUID) |
| 24 | + userId := ctx.Value(identifiers.UserIdentifier).(uuid.UUID) |
25 | 25 |
|
26 |
| - user, err := s.users.Get(ctx, userId) |
27 |
| - if err != nil { |
28 |
| - span.SetStatus(codes.Error, "failed to get user") |
29 |
| - span.RecordError(err) |
30 |
| - common.Throw(w, r, err) |
31 |
| - return |
32 |
| - } |
| 26 | + user, err := s.users.Get(ctx, userId) |
| 27 | + if err != nil { |
| 28 | + span.SetStatus(codes.Error, "failed to get user") |
| 29 | + span.RecordError(err) |
| 30 | + common.Throw(w, r, err) |
| 31 | + return |
| 32 | + } |
33 | 33 |
|
34 |
| - render.Status(r, http.StatusOK) |
35 |
| - render.Respond(w, r, user) |
| 34 | + render.Status(r, http.StatusOK) |
| 35 | + render.Respond(w, r, user) |
36 | 36 | }
|
37 | 37 |
|
38 | 38 | func (s *Server) getUserByID(w http.ResponseWriter, r *http.Request) {
|
39 |
| - //callerId := r.Context().Value(identifiers.UserIdentifier).(uuid.UUID) |
40 |
| - |
41 |
| - userParam := chi.URLParam(r, "user") |
42 |
| - requestedUserId, err := uuid.Parse(userParam) |
43 |
| - user, err := s.users.Get(r.Context(), requestedUserId) |
44 |
| - if err != nil { |
45 |
| - common.Throw(w, r, err) |
46 |
| - return |
47 |
| - } |
48 |
| - |
49 |
| - render.Status(r, http.StatusOK) |
50 |
| - render.Respond(w, r, user) |
| 39 | + //callerId := r.Context().Value(identifiers.UserIdentifier).(uuid.UUID) |
| 40 | + |
| 41 | + userParam := chi.URLParam(r, "user") |
| 42 | + requestedUserId, err := uuid.Parse(userParam) |
| 43 | + if err != nil { |
| 44 | + common.Throw(w, r, err) |
| 45 | + } |
| 46 | + user, err := s.users.Get(r.Context(), requestedUserId) |
| 47 | + if err != nil { |
| 48 | + common.Throw(w, r, err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + render.Status(r, http.StatusOK) |
| 53 | + render.Respond(w, r, user) |
51 | 54 | }
|
52 | 55 |
|
53 | 56 | func (s *Server) updateUser(w http.ResponseWriter, r *http.Request) {
|
54 |
| - ctx, span := tracer.Start(r.Context(), "scrumlr.users.api.update") |
55 |
| - defer span.End() |
56 |
| - log := logger.FromContext(ctx) |
57 |
| - |
58 |
| - user := ctx.Value(identifiers.UserIdentifier).(uuid.UUID) |
59 |
| - |
60 |
| - var body users.UserUpdateRequest |
61 |
| - if err := render.Decode(r, &body); err != nil { |
62 |
| - span.SetStatus(codes.Error, "unable to decode body") |
63 |
| - span.RecordError(err) |
64 |
| - log.Errorw("unable to decode body", "err", err) |
65 |
| - common.Throw(w, r, common.BadRequestError(err)) |
66 |
| - return |
67 |
| - } |
68 |
| - |
69 |
| - body.ID = user |
70 |
| - |
71 |
| - updatedUser, err := s.users.Update(ctx, body) |
72 |
| - if err != nil { |
73 |
| - span.SetStatus(codes.Error, "failed to update user") |
74 |
| - span.RecordError(err) |
75 |
| - common.Throw(w, r, common.InternalServerError) |
76 |
| - return |
77 |
| - } |
78 |
| - |
79 |
| - // because of a import cycle the boards are updated through the session service |
80 |
| - // after a user update. |
81 |
| - updateBoards := sessions.BoardSessionUpdateRequest{ |
82 |
| - User: user, |
83 |
| - } |
84 |
| - |
85 |
| - _, err = s.sessions.UpdateUserBoards(ctx, updateBoards) |
86 |
| - if err != nil { |
87 |
| - span.SetStatus(codes.Error, "failed to update user board") |
88 |
| - span.RecordError(err) |
89 |
| - log.Errorw("Unable to update user boards") |
90 |
| - } |
91 |
| - |
92 |
| - render.Status(r, http.StatusOK) |
93 |
| - render.Respond(w, r, updatedUser) |
| 57 | + ctx, span := tracer.Start(r.Context(), "scrumlr.users.api.update") |
| 58 | + defer span.End() |
| 59 | + log := logger.FromContext(ctx) |
| 60 | + |
| 61 | + user := ctx.Value(identifiers.UserIdentifier).(uuid.UUID) |
| 62 | + |
| 63 | + var body users.UserUpdateRequest |
| 64 | + if err := render.Decode(r, &body); err != nil { |
| 65 | + span.SetStatus(codes.Error, "unable to decode body") |
| 66 | + span.RecordError(err) |
| 67 | + log.Errorw("unable to decode body", "err", err) |
| 68 | + common.Throw(w, r, common.BadRequestError(err)) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + body.ID = user |
| 73 | + |
| 74 | + updatedUser, err := s.users.Update(ctx, body) |
| 75 | + if err != nil { |
| 76 | + span.SetStatus(codes.Error, "failed to update user") |
| 77 | + span.RecordError(err) |
| 78 | + common.Throw(w, r, common.InternalServerError) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // because of a import cycle the boards are updated through the session service |
| 83 | + // after a user update. |
| 84 | + updateBoards := sessions.BoardSessionUpdateRequest{ |
| 85 | + User: user, |
| 86 | + } |
| 87 | + |
| 88 | + _, err = s.sessions.UpdateUserBoards(ctx, updateBoards) |
| 89 | + if err != nil { |
| 90 | + span.SetStatus(codes.Error, "failed to update user board") |
| 91 | + span.RecordError(err) |
| 92 | + log.Errorw("Unable to update user boards") |
| 93 | + } |
| 94 | + |
| 95 | + render.Status(r, http.StatusOK) |
| 96 | + render.Respond(w, r, updatedUser) |
94 | 97 | }
|
0 commit comments