diff --git a/backend/main.go b/backend/main.go index dce1247..0d06b97 100644 --- a/backend/main.go +++ b/backend/main.go @@ -76,6 +76,11 @@ func main() { r.Get("/image", func(w http.ResponseWriter, r *http.Request) { userId := r.Context().Value(USER_ID).(uuid.UUID) + if err != nil { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "You cannot read this") + return + } images, err := userModel.ListWithProperties(r.Context(), userId) if err != nil { @@ -96,6 +101,36 @@ func main() { w.Write(jsonImages) }) + r.Get("/image-properties/{id}", func(w http.ResponseWriter, r *http.Request) { + userId := r.Context().Value(USER_ID).(uuid.UUID) + stringImageId := r.PathValue("id") + + imageId, err := uuid.Parse(stringImageId) + if err != nil { + w.WriteHeader(http.StatusForbidden) + fmt.Fprintf(w, "You cannot read this") + return + } + + image, err := userModel.ListImageWithProperties(r.Context(), userId, imageId) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Something went wrong") + return + } + + jsonImages, err := json.Marshal(models.GetTypedImageProperties([]models.ImageWithProperties{image})[0]) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Could not create JSON response for this image") + return + } + + w.Write(jsonImages) + }) + r.Get("/image/{id}", func(w http.ResponseWriter, r *http.Request) { stringImageId := r.PathValue("id") userId := r.Context().Value(USER_ID).(uuid.UUID) diff --git a/frontend/src/network/index.ts b/frontend/src/network/index.ts index f2c06d0..7fd7a94 100644 --- a/frontend/src/network/index.ts +++ b/frontend/src/network/index.ts @@ -160,6 +160,15 @@ export const getUserImages = async (): Promise => { return parse(getUserImagesResponseValidator, res); }; +export const getImage = async (imageId: string): Promise => { + const request = getBaseAuthorizedRequest({ + path: `image-properties/${imageId}`, + }); + + const res = await fetch(request).then((res) => res.json()); + return parse(dataTypeValidator, res); +}; + export const postLogin = async (email: string): Promise => { const request = getBaseRequest({ path: "login",