github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/organization/handler.go (about) 1 package organization 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "github.com/hellofresh/janus/pkg/errors" 9 "github.com/hellofresh/janus/pkg/render" 10 "github.com/hellofresh/janus/pkg/router" 11 log "github.com/sirupsen/logrus" 12 "go.opencensus.io/trace" 13 ) 14 15 // Handler is the api rest handlers 16 type Handler struct { 17 repo Repository 18 } 19 20 // NewHandler creates a new instance of Handler 21 func NewHandler(repo Repository) *Handler { 22 return &Handler{repo} 23 } 24 25 // Index is the find all handler 26 func (c *Handler) Index() http.HandlerFunc { 27 return func(w http.ResponseWriter, r *http.Request) { 28 _, span := trace.StartSpan(r.Context(), "repo.FindAll") 29 data, err := c.repo.FindAll() 30 span.End() 31 32 if err != nil { 33 errors.Handler(w, r, err) 34 return 35 } 36 37 render.JSON(w, http.StatusOK, data) 38 } 39 } 40 41 // Show is the find by handler 42 func (c *Handler) Show() http.HandlerFunc { 43 return func(w http.ResponseWriter, r *http.Request) { 44 username := router.URLParam(r, "username") 45 _, span := trace.StartSpan(r.Context(), "repo.Show") 46 data, err := c.repo.FindByUsername(username) 47 span.End() 48 49 if err != nil { 50 errors.Handler(w, r, err) 51 return 52 } 53 54 render.JSON(w, http.StatusOK, data) 55 } 56 } 57 58 // ShowOrganization is the find by handler 59 func (c *Handler) ShowOrganization() http.HandlerFunc { 60 return func(w http.ResponseWriter, r *http.Request) { 61 organization := router.URLParam(r, "organization") 62 _, span := trace.StartSpan(r.Context(), "repo.FindOrganization") 63 data, err := c.repo.FindOrganization(organization) 64 span.End() 65 66 if err != nil { 67 errors.Handler(w, r, err) 68 return 69 } 70 71 render.JSON(w, http.StatusOK, data) 72 } 73 } 74 75 // Update is the update handler 76 func (c *Handler) Update() http.HandlerFunc { 77 return func(w http.ResponseWriter, r *http.Request) { 78 var err error 79 80 username := router.URLParam(r, "username") 81 _, span := trace.StartSpan(r.Context(), "repo.FindByUsername") 82 user, err := c.repo.FindByUsername(username) 83 span.End() 84 85 if user == nil { 86 errors.Handler(w, r, ErrUserNotFound) 87 return 88 } 89 90 if err != nil { 91 errors.Handler(w, r, err) 92 return 93 } 94 95 err = json.NewDecoder(r.Body).Decode(user) 96 if err != nil { 97 errors.Handler(w, r, err) 98 return 99 } 100 user.Username = username 101 102 _, span = trace.StartSpan(r.Context(), "repo.Add") 103 err = c.repo.Add(user) 104 span.End() 105 106 if err != nil { 107 errors.Handler(w, r, errors.New(http.StatusBadRequest, err.Error())) 108 return 109 } 110 111 w.WriteHeader(http.StatusOK) 112 } 113 } 114 115 // Create is the create handler 116 func (c *Handler) Create() http.HandlerFunc { 117 return func(w http.ResponseWriter, r *http.Request) { 118 organization := &OrganizationUserAndConfig{} 119 120 err := json.NewDecoder(r.Body).Decode(organization) 121 if nil != err { 122 errors.Handler(w, r, err) 123 return 124 } 125 126 if organization.Organization == "" { 127 err = errors.New(http.StatusBadRequest, "Invalid request body") 128 log.WithError(err).Error("No organization provided. Must provide an organization.") 129 errors.Handler(w, r, err) 130 return 131 } 132 133 _, span := trace.StartSpan(r.Context(), "repo.FindByUsername") 134 _, err = c.repo.FindByUsername(organization.Username) 135 span.End() 136 137 if err != ErrUserNotFound { 138 log.WithError(err).Warn("An error occurred when looking for an organization") 139 errors.Handler(w, r, ErrUserExists) 140 return 141 } 142 143 organizationUser := &Organization{ 144 Username: organization.Username, 145 Organization: organization.Organization, 146 Password: organization.Password, 147 } 148 149 _, span = trace.StartSpan(r.Context(), "repo.Add") 150 err = c.repo.Add(organizationUser) 151 span.End() 152 153 if err != nil { 154 errors.Handler(w, r, errors.New(http.StatusBadRequest, err.Error())) 155 return 156 } 157 158 _, span = trace.StartSpan(r.Context(), "repo.FindOrganization") 159 _, err = c.repo.FindOrganization(organization.Organization) 160 span.End() 161 162 if err == ErrUserNotFound { 163 // create organization if it doesn't already exist 164 organizationConfig := &OrganizationConfig{ 165 Organization: organization.Organization, 166 Priority: organization.Priority, 167 ContentPerDay: organization.ContentPerDay, 168 Config: organization.Config, 169 } 170 171 if organization.Config == nil { 172 organizationConfig.Config = make(map[string]interface{}) 173 } 174 175 _, span = trace.StartSpan(r.Context(), "repo.AddOrganization") 176 err = c.repo.AddOrganization(organizationConfig) 177 span.End() 178 179 if err != nil { 180 errors.Handler(w, r, errors.New(http.StatusBadRequest, err.Error())) 181 return 182 } 183 } 184 185 w.Header().Add("Location", fmt.Sprintf("/credentials/organization/%s", organization.Username)) 186 w.WriteHeader(http.StatusCreated) 187 } 188 } 189 190 // CreateOrganization is the create organization handler 191 func (c *Handler) CreateOrganization() http.HandlerFunc { 192 return func(w http.ResponseWriter, r *http.Request) { 193 organization := &OrganizationConfig{} 194 195 err := json.NewDecoder(r.Body).Decode(organization) 196 if nil != err { 197 errors.Handler(w, r, err) 198 return 199 } 200 201 if organization.Organization == "" { 202 err = errors.New(http.StatusBadRequest, "Invalid request body") 203 log.WithError(err).Error("No organization provided. Must provide an organization.") 204 errors.Handler(w, r, err) 205 return 206 } 207 208 _, span := trace.StartSpan(r.Context(), "repo.FindOrganization") 209 _, err = c.repo.FindOrganization(organization.Organization) 210 span.End() 211 212 if err != ErrUserNotFound { 213 log.WithError(err).Warn("An error occurred when looking for an organization") 214 errors.Handler(w, r, ErrUserExists) 215 return 216 } 217 218 _, span = trace.StartSpan(r.Context(), "repo.Add") 219 err = c.repo.AddOrganization(organization) 220 span.End() 221 222 if err != nil { 223 errors.Handler(w, r, errors.New(http.StatusBadRequest, err.Error())) 224 return 225 } 226 227 w.Header().Add("Location", fmt.Sprintf("/credentials/organization_config/%s", organization.Organization)) 228 w.WriteHeader(http.StatusCreated) 229 } 230 } 231 232 // UpdateOrganization is the update handler 233 func (c *Handler) UpdateOrganization() http.HandlerFunc { 234 return func(w http.ResponseWriter, r *http.Request) { 235 var err error 236 237 organization := router.URLParam(r, "organization") 238 _, span := trace.StartSpan(r.Context(), "repo.FindOrganization") 239 organizationConfig, err := c.repo.FindOrganization(organization) 240 span.End() 241 242 if organizationConfig == nil { 243 errors.Handler(w, r, ErrUserNotFound) 244 return 245 } 246 247 if err != nil { 248 errors.Handler(w, r, err) 249 return 250 } 251 252 err = json.NewDecoder(r.Body).Decode(organizationConfig) 253 if err != nil { 254 errors.Handler(w, r, err) 255 return 256 } 257 organizationConfig.Organization = organization 258 259 _, span = trace.StartSpan(r.Context(), "repo.Add") 260 err = c.repo.AddOrganization(organizationConfig) 261 span.End() 262 263 if err != nil { 264 errors.Handler(w, r, errors.New(http.StatusBadRequest, err.Error())) 265 return 266 } 267 268 w.WriteHeader(http.StatusOK) 269 } 270 } 271 272 // Delete is the delete handler 273 func (c *Handler) Delete() http.HandlerFunc { 274 return func(w http.ResponseWriter, r *http.Request) { 275 username := router.URLParam(r, "username") 276 277 _, span := trace.StartSpan(r.Context(), "repo.Remove") 278 err := c.repo.Remove(username) 279 span.End() 280 281 if err != nil { 282 errors.Handler(w, r, err) 283 return 284 } 285 286 w.WriteHeader(http.StatusNoContent) 287 } 288 }