Skip to content

Commit

Permalink
sync endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalraja committed Jan 4, 2022
1 parent 7957539 commit d4ef511
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 3 deletions.
2 changes: 2 additions & 0 deletions backend/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func NewServer() *Server {

sr.HandleFunc("/syncs", srv.handleCreateSyncLocation()).Methods(http.MethodPost)
sr.HandleFunc("/syncs/{id}", srv.handleSaveSyncLocation()).Methods(http.MethodPut)
sr.HandleFunc("/syncs/{id}", srv.handleDeleteSync()).Methods(http.MethodDelete)
sr.HandleFunc("/syncs", srv.handleGetSync()).Methods(http.MethodGet)

sr.HandleFunc("/media", srv.handleGetAllMedia()).Methods(http.MethodGet)
sr.HandleFunc("/media/{id}", srv.handleGetMedia()).Methods(http.MethodGet)
Expand Down
32 changes: 32 additions & 0 deletions backend/api/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func (s *Server) handleCreateSyncLocation() http.HandlerFunc {
}
ctx := r.Context()
u := s.currentUser(ctx)
if u == nil {
return errAuth
}
if err := req.Save(u); err != nil {
return err
}
Expand All @@ -31,6 +34,9 @@ func (s *Server) handleSaveSyncLocation() http.HandlerFunc {
}
ctx := r.Context()
u := s.currentUser(ctx)
if u == nil {
return errAuth
}
locID := util.Atoi64(mux.Vars(r)["id"])
loc, err := model.GetSyncLocation(u.ID, locID)
if err != nil {
Expand All @@ -46,3 +52,29 @@ func (s *Server) handleSaveSyncLocation() http.HandlerFunc {
return loc
})
}

func (s *Server) handleGetSync() http.HandlerFunc {
return s.handler(func(r *http.Request) interface{} {
ctx := r.Context()
u := s.currentUser(ctx)
if u == nil {
return errAuth
}
syncs, err := model.GetSyncs(u.ID)
if err != nil {
return err
}
return s.cursor(syncs, 1)
})
}

func (s *Server) handleDeleteSync() http.HandlerFunc {
return s.handler(func(r *http.Request) interface{} {
u := s.currentUser(r.Context())
if u == nil {
return errAuth
}
locID := util.Atoi64(mux.Vars(r)["id"])
return model.DeleteSyncByID(u.ID, locID)
})
}
30 changes: 28 additions & 2 deletions backend/model/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ func saveSyncLocation(userID int64, syncLoc *SyncLocation) error {
syncLoc.ID = id
}
} else {
args = append(args, syncLoc.Deleted)
args = append(args, syncLoc.ID)
_, err = db.Exec(`
UPDATE sync_location SET
name = $1,
stype = $2,
config = $3
WHERE id = $4
config = $3,
deleted = $4
WHERE id = $5
`, args...)
}
if err != nil {
Expand All @@ -87,3 +89,27 @@ func GetSyncLocation(userID int64, locID int64) (*SyncLocation, error) {
}
return loc, nil
}

func GetSyncs(userID int64) ([]SyncLocation, error) {
db, err := getDB(userID)
if err != nil {
return nil, fmt.Errorf("GetSyncs getDB error: %v", err)
}
syncs := []SyncLocation{}
if err = db.Select(&syncs, `SELECT * FROM sync_location WHERE deleted IS NULL`); err != nil {
return nil, fmt.Errorf("GetSyncs select error %v", err)
}
return syncs, nil
}

func DeleteSyncByID(userID int64, locID int64) error {
db, err := getDB(userID)
if err != nil {
return fmt.Errorf("DeleteSyncByID getDB error: %v", err)
}
_, err = db.Exec(`DELETE FROM sync_location WHERE id = ?`, locID)
if err != nil {
return fmt.Errorf("DeleteSyncByID exec error %v", err)
}
return nil
}
37 changes: 36 additions & 1 deletion backend/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -79,11 +80,45 @@ func (m *Meta) Value() (driver.Value, error) {
return json.Marshal(m)
}

func (t *DateTime) Scan(src interface{}) error {
switch tt := src.(type) {
case time.Time:
*t = DateTime(tt)
return nil
case string:
if tt == "" {
return nil
}
dt, err := time.Parse(util.DateTimeFormat, tt)
if err == nil {
*t = DateTime(dt)
}
return err
default:
log.Println("Type", reflect.TypeOf(src))
return ErrInvalidType
}
}

func (t *DateTime) Value() (driver.Value, error) {
return time.Time(*t).Format(util.DateTimeFormat), nil
}

func (t DateTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format(util.DateTimeFormat))
return []byte(stamp), nil
}

func (t *DateTime) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
dt, err := time.Parse(util.DateTimeFormat, s)
if err == nil {
ddt := DateTime(dt)
*t = ddt
}
return err
}

func (ed *ExifData) Walk(name exif.FieldName, tag *tiff.Tag) error {
if ed.Data == nil {
ed.Data = make(map[string]*tiff.Tag)
Expand Down Expand Up @@ -406,7 +441,7 @@ func GetUsers() ([]User, error) {
}
users := []User{}
if err = db.Select(&users, `SELECT * FROM user`); err != nil {
return nil, fmt.Errorf("GetUsers select error")
return nil, fmt.Errorf("GetUsers select error %v", err)
}
return users, nil
}
Expand Down
43 changes: 43 additions & 0 deletions tests/api.http
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,49 @@ Authorization: {{auth}}
Content-Type: application/json


###

POST {{baseUrl}}/api/syncs
Authorization: {{auth}}
Content-Type: application/json

{
"name": "TG 1",
"type": "telegram",
"config": {
"token": "",
"channel": ""
}
}

###

PUT {{baseUrl}}/api/syncs/1
Authorization: {{auth}}
Content-Type: application/json

{
"name": "TG 2",
"type": "telegram",
"config": {
"token": "",
"channel": ""
},
"deleted": "2021-01-02 15:04:05"
}

###

GET {{baseUrl}}/api/syncs
Authorization: {{auth}}
Content-Type: application/json

###

DELETE {{baseUrl}}/api/syncs/3
Authorization: {{auth}}
Content-Type: application/json

###

POST {{baseUrl}}/api/upload/dir
Expand Down

0 comments on commit d4ef511

Please sign in to comment.