mirror of
https://github.com/documize/community.git
synced 2025-08-05 21:45:28 +02:00
moving endpoints to new API (WIP)
This commit is contained in:
parent
27640dffc4
commit
72b14def6d
36 changed files with 1371 additions and 472 deletions
|
@ -18,17 +18,25 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/request"
|
||||
"github.com/documize/community/core/response"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/eventing"
|
||||
"github.com/documize/community/model/audit"
|
||||
"github.com/documize/community/model/pin"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
Store *domain.Store
|
||||
}
|
||||
|
||||
// Add saves pinned item.
|
||||
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
||||
method := "pin.Add"
|
||||
s := domain.NewContext(h.Runtime, r)
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
|
@ -41,7 +49,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if !s.Context.Authenticated {
|
||||
if !ctx.Authenticated {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
@ -53,7 +61,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var pin Pin
|
||||
var pin pin.Pin
|
||||
err = json.Unmarshal(body, &pin)
|
||||
if err != nil {
|
||||
response.WriteBadRequestError(w, method, "pin")
|
||||
|
@ -61,31 +69,31 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
pin.RefID = uniqueid.Generate()
|
||||
pin.OrgID = s.Context.OrgID
|
||||
pin.UserID = s.Context.UserID
|
||||
pin.OrgID = ctx.OrgID
|
||||
pin.UserID = ctx.UserID
|
||||
pin.Pin = strings.TrimSpace(pin.Pin)
|
||||
if len(pin.Pin) > 20 {
|
||||
pin.Pin = pin.Pin[0:20]
|
||||
}
|
||||
|
||||
s.Context.Transaction, err = h.Runtime.Db.Beginx()
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = Add(s, pin)
|
||||
err = h.Store.Pin.Add(ctx, pin)
|
||||
if err != nil {
|
||||
s.Context.Transaction.Rollback()
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
eventing.Record(s, eventing.EventTypePinAdd)
|
||||
h.Store.Audit.Record(ctx, audit.EventTypePinAdd)
|
||||
|
||||
s.Context.Transaction.Commit()
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
newPin, err := GetPin(s, pin.RefID)
|
||||
newPin, err := h.Store.Pin.GetPin(ctx, pin.RefID)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
|
@ -97,7 +105,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
// GetUserPins returns users' pins.
|
||||
func (h *Handler) GetUserPins(w http.ResponseWriter, r *http.Request) {
|
||||
method := "pin.GetUserPins"
|
||||
s := domain.NewContext(h.Runtime, r)
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
|
@ -105,19 +113,19 @@ func (h *Handler) GetUserPins(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if !s.Context.Authenticated {
|
||||
if !ctx.Authenticated {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
pins, err := GetUserPins(s, userID)
|
||||
pins, err := h.Store.Pin.GetUserPins(ctx, userID)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
pins = []Pin{}
|
||||
pins = []pin.Pin{}
|
||||
}
|
||||
|
||||
response.WriteJSON(w, pins)
|
||||
|
@ -126,7 +134,7 @@ func (h *Handler) GetUserPins(w http.ResponseWriter, r *http.Request) {
|
|||
// DeleteUserPin removes saved user pin.
|
||||
func (h *Handler) DeleteUserPin(w http.ResponseWriter, r *http.Request) {
|
||||
method := "pin.DeleteUserPin"
|
||||
s := domain.NewContext(h.Runtime, r)
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
|
@ -145,28 +153,28 @@ func (h *Handler) DeleteUserPin(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if s.Context.UserID != userID {
|
||||
if ctx.UserID != userID {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
s.Context.Transaction, err = h.Runtime.Db.Beginx()
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = DeletePin(s, pinID)
|
||||
_, err = h.Store.Pin.DeletePin(ctx, pinID)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
s.Context.Transaction.Rollback()
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
eventing.Record(s, eventing.EventTypePinDelete)
|
||||
h.Store.Audit.Record(ctx, audit.EventTypePinDelete)
|
||||
|
||||
s.Context.Transaction.Commit()
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
@ -174,7 +182,7 @@ func (h *Handler) DeleteUserPin(w http.ResponseWriter, r *http.Request) {
|
|||
// UpdatePinSequence records order of pinned items.
|
||||
func (h *Handler) UpdatePinSequence(w http.ResponseWriter, r *http.Request) {
|
||||
method := "pin.DeleteUserPin"
|
||||
s := domain.NewContext(h.Runtime, r)
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
userID := request.Param(r, "userID")
|
||||
if len(userID) == 0 {
|
||||
|
@ -187,7 +195,7 @@ func (h *Handler) UpdatePinSequence(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if !s.Context.Authenticated {
|
||||
if !ctx.Authenticated {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
@ -207,26 +215,26 @@ func (h *Handler) UpdatePinSequence(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
s.Context.Transaction, err = h.Runtime.Db.Beginx()
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range pins {
|
||||
err = UpdatePinSequence(s, v, k+1)
|
||||
err = h.Store.Pin.UpdatePinSequence(ctx, v, k+1)
|
||||
if err != nil {
|
||||
s.Context.Transaction.Rollback()
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
eventing.Record(s, eventing.EventTypePinResequence)
|
||||
h.Store.Audit.Record(ctx, audit.EventTypePinResequence)
|
||||
|
||||
s.Context.Transaction.Commit()
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
newPins, err := GetUserPins(s, userID)
|
||||
newPins, err := h.Store.Pin.GetUserPins(ctx, userID)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
||||
//
|
||||
// This software (Documize Community Edition) is licensed under
|
||||
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
//
|
||||
// You can operate outside the AGPL restrictions by purchasing
|
||||
// Documize Enterprise Edition and obtaining a commercial license
|
||||
// by contacting <sales@documize.com>.
|
||||
//
|
||||
// https://documize.com
|
||||
|
||||
package pin
|
||||
|
||||
import (
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/domain"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
}
|
||||
|
||||
// Pin defines a saved link to a document or space
|
||||
type Pin struct {
|
||||
domain.BaseEntity
|
||||
OrgID string `json:"orgId"`
|
||||
UserID string `json:"userId"`
|
||||
FolderID string `json:"folderId"`
|
||||
DocumentID string `json:"documentId"`
|
||||
Pin string `json:"pin"`
|
||||
Sequence int `json:"sequence"`
|
||||
}
|
|
@ -15,17 +15,23 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/api/entity"
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store/mysql"
|
||||
"github.com/documize/community/model/pin"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Scope provides data access to MySQL.
|
||||
type Scope struct {
|
||||
Runtime *env.Runtime
|
||||
}
|
||||
|
||||
// Add saves pinned item.
|
||||
func Add(s domain.StoreContext, pin Pin) (err error) {
|
||||
row := s.Runtime.Db.QueryRow("SELECT max(sequence) FROM pin WHERE orgid=? AND userid=?", s.Context.OrgID, s.Context.UserID)
|
||||
func (s Scope) Add(ctx domain.RequestContext, pin pin.Pin) (err error) {
|
||||
row := s.Runtime.Db.QueryRow("SELECT max(sequence) FROM pin WHERE orgid=? AND userid=?", ctx.OrgID, ctx.UserID)
|
||||
var maxSeq int
|
||||
err = row.Scan(&maxSeq)
|
||||
|
||||
|
@ -37,7 +43,7 @@ func Add(s domain.StoreContext, pin Pin) (err error) {
|
|||
pin.Revised = time.Now().UTC()
|
||||
pin.Sequence = maxSeq + 1
|
||||
|
||||
stmt, err := s.Context.Transaction.Preparex("INSERT INTO pin (refid, orgid, userid, labelid, documentid, pin, sequence, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
stmt, err := ctx.Transaction.Preparex("INSERT INTO pin (refid, orgid, userid, labelid, documentid, pin, sequence, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -55,7 +61,7 @@ func Add(s domain.StoreContext, pin Pin) (err error) {
|
|||
}
|
||||
|
||||
// GetPin returns requested pinned item.
|
||||
func GetPin(s domain.StoreContext, id string) (pin Pin, err error) {
|
||||
func (s Scope) GetPin(ctx domain.RequestContext, id string) (pin pin.Pin, err error) {
|
||||
stmt, err := s.Runtime.Db.Preparex("SELECT id, refid, orgid, userid, labelid as folderid, documentid, pin, sequence, created, revised FROM pin WHERE orgid=? AND refid=?")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
|
@ -64,7 +70,7 @@ func GetPin(s domain.StoreContext, id string) (pin Pin, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
err = stmt.Get(&pin, s.Context.OrgID, id)
|
||||
err = stmt.Get(&pin, ctx.OrgID, id)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("execute select for pin %s", id))
|
||||
return
|
||||
|
@ -74,11 +80,11 @@ func GetPin(s domain.StoreContext, id string) (pin Pin, err error) {
|
|||
}
|
||||
|
||||
// GetUserPins returns pinned items for specified user.
|
||||
func GetUserPins(s domain.StoreContext, userID string) (pins []Pin, err error) {
|
||||
err = s.Runtime.Db.Select(&pins, "SELECT id, refid, orgid, userid, labelid as folderid, documentid, pin, sequence, created, revised FROM pin WHERE orgid=? AND userid=? ORDER BY sequence", s.Context.OrgID, userID)
|
||||
func (s Scope) GetUserPins(ctx domain.RequestContext, userID string) (pins []pin.Pin, err error) {
|
||||
err = s.Runtime.Db.Select(&pins, "SELECT id, refid, orgid, userid, labelid as folderid, documentid, pin, sequence, created, revised FROM pin WHERE orgid=? AND userid=? ORDER BY sequence", ctx.OrgID, userID)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("execute select pins for org %s and user %s", s.Context.OrgID, userID))
|
||||
err = errors.Wrap(err, fmt.Sprintf("execute select pins for org %s and user %s", ctx.OrgID, userID))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -86,11 +92,11 @@ func GetUserPins(s domain.StoreContext, userID string) (pins []Pin, err error) {
|
|||
}
|
||||
|
||||
// UpdatePin updates existing pinned item.
|
||||
func UpdatePin(s domain.StoreContext, pin entity.Pin) (err error) {
|
||||
func (s Scope) UpdatePin(ctx domain.RequestContext, pin pin.Pin) (err error) {
|
||||
pin.Revised = time.Now().UTC()
|
||||
|
||||
var stmt *sqlx.NamedStmt
|
||||
stmt, err = s.Context.Transaction.PrepareNamed("UPDATE pin SET labelid=:folderid, documentid=:documentid, pin=:pin, sequence=:sequence, revised=:revised WHERE orgid=:orgid AND refid=:refid")
|
||||
stmt, err = ctx.Transaction.PrepareNamed("UPDATE pin SET labelid=:folderid, documentid=:documentid, pin=:pin, sequence=:sequence, revised=:revised WHERE orgid=:orgid AND refid=:refid")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -108,8 +114,8 @@ func UpdatePin(s domain.StoreContext, pin entity.Pin) (err error) {
|
|||
}
|
||||
|
||||
// UpdatePinSequence updates existing pinned item sequence number
|
||||
func UpdatePinSequence(s domain.StoreContext, pinID string, sequence int) (err error) {
|
||||
stmt, err := s.Context.Transaction.Preparex("UPDATE pin SET sequence=?, revised=? WHERE orgid=? AND userid=? AND refid=?")
|
||||
func (s Scope) UpdatePinSequence(ctx domain.RequestContext, pinID string, sequence int) (err error) {
|
||||
stmt, err := ctx.Transaction.Preparex("UPDATE pin SET sequence=?, revised=? WHERE orgid=? AND userid=? AND refid=?")
|
||||
defer streamutil.Close(stmt)
|
||||
|
||||
if err != nil {
|
||||
|
@ -117,7 +123,7 @@ func UpdatePinSequence(s domain.StoreContext, pinID string, sequence int) (err e
|
|||
return
|
||||
}
|
||||
|
||||
_, err = stmt.Exec(sequence, time.Now().UTC(), s.Context.OrgID, s.Context.UserID, pinID)
|
||||
_, err = stmt.Exec(sequence, time.Now().UTC(), ctx.OrgID, ctx.UserID, pinID)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, fmt.Sprintf("execute pin sequence update %s", pinID))
|
||||
return
|
||||
|
@ -127,19 +133,19 @@ func UpdatePinSequence(s domain.StoreContext, pinID string, sequence int) (err e
|
|||
}
|
||||
|
||||
// DeletePin removes folder from the store.
|
||||
func DeletePin(s domain.StoreContext, id string) (rows int64, err error) {
|
||||
func (s Scope) DeletePin(ctx domain.RequestContext, id string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteConstrained(s.Context.Transaction, "pin", s.Context.OrgID, id)
|
||||
return b.DeleteConstrained(ctx.Transaction, "pin", ctx.OrgID, id)
|
||||
}
|
||||
|
||||
// DeletePinnedSpace removes any pins for specified space.
|
||||
func DeletePinnedSpace(s domain.StoreContext, spaceID string) (rows int64, err error) {
|
||||
func (s Scope) DeletePinnedSpace(ctx domain.RequestContext, spaceID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteWhere(s.Context.Transaction, fmt.Sprintf("DELETE FROM pin WHERE orgid=\"%s\" AND labelid=\"%s\"", s.Context.OrgID, spaceID))
|
||||
return b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM pin WHERE orgid=\"%s\" AND labelid=\"%s\"", ctx.OrgID, spaceID))
|
||||
}
|
||||
|
||||
// DeletePinnedDocument removes any pins for specified document.
|
||||
func DeletePinnedDocument(s domain.StoreContext, documentID string) (rows int64, err error) {
|
||||
func (s Scope) DeletePinnedDocument(ctx domain.RequestContext, documentID string) (rows int64, err error) {
|
||||
b := mysql.BaseQuery{}
|
||||
return b.DeleteWhere(s.Context.Transaction, fmt.Sprintf("DELETE FROM pin WHERE orgid=\"%s\" AND documentid=\"%s\"", s.Context.OrgID, documentID))
|
||||
return b.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM pin WHERE orgid=\"%s\" AND documentid=\"%s\"", ctx.OrgID, documentID))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue