mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-05-06 04:42:36 +02:00
Add getUserProfile and database.IsBanned
This commit is contained in:
parent
44eb1e1fa6
commit
53a764e8bb
13 changed files with 328 additions and 163 deletions
98
service/database/db-likes.go
Normal file
98
service/database/db-likes.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/notherealmarco/WASAPhoto/service/database/db_errors"
|
||||
"github.com/notherealmarco/WASAPhoto/service/structures"
|
||||
)
|
||||
|
||||
// Get the list of users who liked a photo
|
||||
func (db *appdbimpl) GetPhotoLikes(uid string, photo int64) (QueryResult, *[]structures.UIDName, error) {
|
||||
|
||||
// Check if the photo exists, as it could exist but have no likes
|
||||
exists, err := db.photoExists(uid, photo)
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, nil, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return ERR_NOT_FOUND, nil, nil
|
||||
}
|
||||
|
||||
rows, err := db.c.Query(`SELECT "users"."uid", "users"."name" FROM "likes", "users"
|
||||
WHERE "likes"."photo_id" = ?
|
||||
AND "likes"."user" = "users"."uid"`, photo)
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, nil, err
|
||||
}
|
||||
|
||||
likes := make([]structures.UIDName, 0)
|
||||
for rows.Next() {
|
||||
var uid string
|
||||
var name string
|
||||
err = rows.Scan(&uid, &name)
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, nil, err
|
||||
}
|
||||
likes = append(likes, structures.UIDName{UID: uid, Name: name})
|
||||
}
|
||||
|
||||
return SUCCESS, &likes, nil
|
||||
}
|
||||
|
||||
// Like a photo
|
||||
func (db *appdbimpl) LikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error) {
|
||||
|
||||
// Check if the photo exists, as API specification requires
|
||||
// photos to be identified also by the user who posted them.
|
||||
// But our DB implementation only requires the photo id.
|
||||
exists, err := db.photoExists(uid, photo)
|
||||
if err != nil || !exists {
|
||||
return ERR_NOT_FOUND, err
|
||||
}
|
||||
|
||||
_, err = db.c.Exec(`PRAGMA foreign_keys = ON;
|
||||
INSERT INTO "likes" ("user", "photo_id") VALUES (?, ?)`, liker_uid, photo)
|
||||
|
||||
// The photo exists, but the user already liked it
|
||||
if db_errors.UniqueViolation(err) {
|
||||
return ERR_EXISTS, nil
|
||||
}
|
||||
|
||||
if db_errors.ForeignKeyViolation(err) {
|
||||
return ERR_NOT_FOUND, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, err
|
||||
}
|
||||
return SUCCESS, nil
|
||||
}
|
||||
|
||||
// Unlike a photo
|
||||
func (db *appdbimpl) UnlikePhoto(uid string, photo int64, liker_uid string) (QueryResult, error) {
|
||||
|
||||
// Check if the photo exists, as API specification requires
|
||||
// photos to be identified also by the user who posted them.
|
||||
// But our DB implementation only requires the photo id.
|
||||
exists, err := db.photoExists(uid, photo)
|
||||
if err != nil || !exists {
|
||||
return ERR_NOT_FOUND, err
|
||||
}
|
||||
|
||||
res, err := db.c.Exec(`DELETE FROM "likes" WHERE "user" = ? AND "photo_id" = ?`, liker_uid, photo)
|
||||
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, err
|
||||
}
|
||||
|
||||
rows, err := res.RowsAffected()
|
||||
|
||||
if err != nil {
|
||||
return ERR_INTERNAL, err
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
return ERR_NOT_FOUND, nil
|
||||
}
|
||||
return SUCCESS, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue