WASAPhoto/service/api/likes.go

114 lines
2.9 KiB
Go
Raw Permalink Normal View History

package api
import (
"encoding/json"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
"github.com/notherealmarco/WASAPhoto/service/api/authorization"
"github.com/notherealmarco/WASAPhoto/service/api/helpers"
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
"github.com/notherealmarco/WASAPhoto/service/database"
)
func (rt *_router) GetLikes(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
2022-11-22 23:41:52 +01:00
if !authorization.SendErrorIfNotLoggedIn(ctx.Auth.Authorized, rt.db, w, rt.baseLogger) {
return
}
// get the user id from the url
uid := ps.ByName("user_id")
photo_id, err := strconv.ParseInt(ps.ByName("photo_id"), 10, 64)
if err != nil {
helpers.SendBadRequest(w, "Invalid photo id", rt.baseLogger)
return
}
2022-11-22 15:21:33 +01:00
// get limits, or use defaults
start_index, limit, err := helpers.GetLimits(r.URL.Query())
if err != nil {
helpers.SendBadRequest(w, "Invalid start_index or limit", rt.baseLogger)
return
}
// send 404 if the user does not exist
if !helpers.VerifyUserOrNotFound(rt.db, uid, w, rt.baseLogger) {
return
}
// get the user's likes
2022-11-22 15:21:33 +01:00
success, likes, err := rt.db.GetPhotoLikes(uid, photo_id, ctx.Auth.GetUserID(), start_index, limit)
if err != nil {
helpers.SendInternalError(err, "Database error: GetLikes", w, rt.baseLogger)
return
}
if success == database.ERR_NOT_FOUND {
helpers.SendNotFound(w, "User or photo not found", rt.baseLogger)
return
}
// send the response
err = json.NewEncoder(w).Encode(likes)
if err != nil {
helpers.SendInternalError(err, "Error encoding response", w, rt.baseLogger)
return
}
}
func (rt *_router) PutDeleteLike(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) {
uid := ps.ByName("user_id")
photo_id, err := strconv.ParseInt(ps.ByName("photo_id"), 10, 64)
if err != nil {
helpers.SendBadRequestError(err, "Bad photo_id", w, rt.baseLogger)
return
}
liker_uid := ps.ByName("liker_uid")
2022-11-20 19:53:24 +01:00
if !authorization.SendAuthorizationError(ctx.Auth.UserAuthorized, liker_uid, rt.db, w, rt.baseLogger, http.StatusBadRequest) {
return
}
var success database.QueryResult
// If the request is a PUT, then we like the photo
if r.Method == "PUT" {
success, err = rt.db.LikePhoto(uid, photo_id, liker_uid)
} else { // Request is a DELETE, so we unlike the photo
success, err = rt.db.UnlikePhoto(uid, photo_id, liker_uid)
}
if err != nil {
helpers.SendInternalError(err, "Database error", w, rt.baseLogger)
return
}
if success == database.ERR_NOT_FOUND {
2022-11-20 19:43:52 +01:00
helpers.SendNotFound(w, "Resource not found", rt.baseLogger)
return
}
// User already liked the photo
if success == database.ERR_EXISTS {
w.WriteHeader(http.StatusNoContent)
return
}
2022-11-20 19:42:15 +01:00
if r.Method == "PUT" {
// User liked the photo successfully
helpers.SendStatus(http.StatusCreated, w, "Success", rt.baseLogger)
} else {
// User unliked the photo successfully
w.WriteHeader(http.StatusNoContent)
}
}