From 5f3d4df33adc30fdeb8cd0ad3c184e839b018011 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Fri, 18 Nov 2022 10:54:14 +0100 Subject: [PATCH] Add database handlers --- service/api/session.go | 35 +++++++++++++++++++++++++++++++ service/database/database.go | 13 +++++++++++- service/database/db-photos.go | 39 +++++++++++++++++++++++++++++++++-- service/database/db-users.go | 10 +++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 service/api/session.go diff --git a/service/api/session.go b/service/api/session.go new file mode 100644 index 0000000..bf6177c --- /dev/null +++ b/service/api/session.go @@ -0,0 +1,35 @@ +package api + +import ( + "encoding/json" + "net/http" + + "github.com/julienschmidt/httprouter" + "github.com/notherealmarco/WASAPhoto/service/api/reqcontext" +) + +type _reqbody struct { + Name string `json:"name"` +} + +type _respbody struct { + UID string `json:"uid"` +} + +// getContextReply is an example of HTTP endpoint that returns "Hello World!" as a plain text. The signature of this +// handler accepts a reqcontext.RequestContext (see httpRouterHandler). +func (rt *_router) PostSession(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) { + + var request _reqbody + json.NewDecoder(r.Body).Decode(&request) //todo: capire se serve close + + uid, err := rt.db.GetUserID(request.Name) + + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Header().Set("content-type", "application/json") + json.NewEncoder(w).Encode(_respbody{UID: uid}) +} diff --git a/service/database/database.go b/service/database/database.go index 2a48855..b3bd8d6 100644 --- a/service/database/database.go +++ b/service/database/database.go @@ -38,8 +38,19 @@ import ( // AppDatabase is the high level interface for the DB type AppDatabase interface { - GetName() (string, error) + GetUserID(name string) (string, error) SetName(name string) error + CreateUser(uid string, name string) error + FollowUser(uid string, follow string) error + UnfollowUser(uid string, unfollow string) error + BanUser(uid string, ban string) error + UnbanUser(uid string, unban string) error + + PostPhoto(uid string) (int64, error) + LikePhoto(uid string, photo int64) error + UnlikePhoto(uid string, photo int64) error + + GetUserProfile(uid string) (*UserProfile, error) Ping() error } diff --git a/service/database/db-photos.go b/service/database/db-photos.go index f531546..a043a75 100644 --- a/service/database/db-photos.go +++ b/service/database/db-photos.go @@ -11,8 +11,8 @@ type Photo struct { type UserProfile struct { UID string Name string - Following []string - Followers []string + Following int64 + Followers int64 Photos []Photo } @@ -37,7 +37,42 @@ func (db *appdbimpl) GetUserProfile(uid string) (*UserProfile, error) { if err != nil { return nil, err } + // Get followers + var followers int64 + err = db.c.QueryRow(`SELECT COUNT(*) FROM "follows" WHERE "followed" = ?`, uid).Scan(&followers) + + // Get following users + var following int64 + err = db.c.QueryRow(`SELECT COUNT(*) FROM "follows" WHERE "follower" = ?`, uid).Scan(&following) + + // Get photos + rows, err := db.c.Query(`SELECT "photos.id", "photos.date", + COUNT("likes.user") AS "likes", + COUNT("comments.user") AS "comments" + FROM "photos", "likes", "comments" + WHERE "likes.photo_id" = "photos.id" + AND "comments.photo" = "photos.id" + AND "user" = ?`, uid) + if err != nil { + return nil, err + } + + var photos []Photo + for rows.Next() { + var id int64 + var date string + var likes int64 + var comments int64 + err = rows.Scan(&id, &date, &likes, &comments) + if err != nil { + return nil, err + } + photo_data := Photo{id, likes, comments} + photos = append(photos, photo_data) + } + + return &UserProfile{uid, name, followers, following, photos}, nil } // Like a photo diff --git a/service/database/db-users.go b/service/database/db-users.go index 0093436..95e3c20 100644 --- a/service/database/db-users.go +++ b/service/database/db-users.go @@ -1,5 +1,15 @@ package database +//Check if user exists and if exists return the user id by username +//todo + +// Get user id by username +func (db *appdbimpl) GetUserID(name string) (string, error) { + var uid string + err := db.c.QueryRow(`SELECT "uid" FROM "users" WHERE "name" = ?`, name).Scan(&uid) + return uid, err +} + // Create a new user func (db *appdbimpl) CreateUser(uid string, name string) error { _, err := db.c.Exec(`INSERT INTO "users" ("uid", "name") VALUES (?, ?)`, uid, name)