mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-03-13 13:35:23 +01:00
Add database handlers
This commit is contained in:
parent
37b7fede91
commit
5f3d4df33a
4 changed files with 94 additions and 3 deletions
35
service/api/session.go
Normal file
35
service/api/session.go
Normal file
|
@ -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})
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue