mirror of
https://github.com/documize/community.git
synced 2025-08-05 05:25:27 +02:00
Per space label, icon, description
Labels introduce visual grouping and filtering of spaces.
This commit is contained in:
parent
fe8068965c
commit
a211ba051a
106 changed files with 3280 additions and 1008 deletions
201
domain/label/endpoint.go
Normal file
201
domain/label/endpoint.go
Normal file
|
@ -0,0 +1,201 @@
|
|||
// 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 label
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
"github.com/documize/community/core/request"
|
||||
"github.com/documize/community/core/response"
|
||||
"github.com/documize/community/core/streamutil"
|
||||
"github.com/documize/community/core/uniqueid"
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/audit"
|
||||
"github.com/documize/community/model/label"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
type Handler struct {
|
||||
Runtime *env.Runtime
|
||||
Store *store.Store
|
||||
}
|
||||
|
||||
// Add space label to the store.
|
||||
func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
||||
method := "label.Add"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
if !h.Runtime.Product.IsValid(ctx) {
|
||||
response.WriteBadLicense(w)
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
defer streamutil.Close(r.Body)
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
response.WriteBadRequestError(w, method, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
l := label.Label{}
|
||||
err = json.Unmarshal(body, &l)
|
||||
if err != nil {
|
||||
response.WriteBadRequestError(w, method, err.Error())
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
l.RefID = uniqueid.Generate()
|
||||
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.Store.Label.Add(ctx, l)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeLabelAdd)
|
||||
|
||||
response.WriteJSON(w, l)
|
||||
}
|
||||
|
||||
// Get returns all space labels.
|
||||
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
method := "label.Get"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
l, err := h.Store.Label.Get(ctx)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
if len(l) == 0 {
|
||||
l = []label.Label{}
|
||||
}
|
||||
|
||||
response.WriteJSON(w, l)
|
||||
}
|
||||
|
||||
// Update persists label name/color changes.
|
||||
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
method := "label.Update"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
if !ctx.Administrator {
|
||||
response.WriteForbiddenError(w)
|
||||
return
|
||||
}
|
||||
|
||||
labelID := request.Param(r, "labelID")
|
||||
if len(labelID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "labelID")
|
||||
return
|
||||
}
|
||||
|
||||
defer streamutil.Close(r.Body)
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
response.WriteBadRequestError(w, method, "Bad payload")
|
||||
return
|
||||
}
|
||||
|
||||
l := label.Label{}
|
||||
err = json.Unmarshal(body, &l)
|
||||
if err != nil {
|
||||
response.WriteBadRequestError(w, method, "Bad payload")
|
||||
return
|
||||
}
|
||||
|
||||
l.RefID = labelID
|
||||
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.Store.Label.Update(ctx, l)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeLabelUpdate)
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
||||
|
||||
// Delete removes space label from store and
|
||||
// removes label association from spaces.
|
||||
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
method := "label.Delete"
|
||||
ctx := domain.GetRequestContext(r)
|
||||
|
||||
labelID := request.Param(r, "labelID")
|
||||
if len(labelID) == 0 {
|
||||
response.WriteMissingDataError(w, method, "labelID")
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.Store.Label.Delete(ctx, labelID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.Store.Label.RemoveReference(ctx, labelID)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeLabelDelete)
|
||||
|
||||
response.WriteEmpty(w)
|
||||
}
|
106
domain/label/store.go
Normal file
106
domain/label/store.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
// 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 label
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/domain"
|
||||
"github.com/documize/community/domain/store"
|
||||
"github.com/documize/community/model/label"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Store provides data access to section template information.
|
||||
type Store struct {
|
||||
store.Context
|
||||
store.LabelStorer
|
||||
}
|
||||
|
||||
// Add saves space label to store.
|
||||
func (s Store) Add(ctx domain.RequestContext, l label.Label) (err error) {
|
||||
l.OrgID = ctx.OrgID
|
||||
l.Created = time.Now().UTC()
|
||||
l.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_space_label (c_refid, c_orgid, c_name, c_color, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?)"),
|
||||
l.RefID, l.OrgID, l.Name, l.Color, l.Created, l.Revised)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute insert label")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns all space labels from store.
|
||||
func (s Store) Get(ctx domain.RequestContext) (l []label.Label, err error) {
|
||||
err = s.Runtime.Db.Select(&l, s.Bind(`
|
||||
SELECT id, c_refid as refid,
|
||||
c_orgid as orgid,
|
||||
c_name AS name, c_color AS color,
|
||||
c_created AS created, c_revised AS revised
|
||||
FROM dmz_space_label
|
||||
WHERE c_orgid=?`),
|
||||
ctx.OrgID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute select label")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Update persists space label changes to the store.
|
||||
func (s Store) Update(ctx domain.RequestContext, l label.Label) (err error) {
|
||||
l.Revised = time.Now().UTC()
|
||||
|
||||
_, err = ctx.Transaction.NamedExec(s.Bind(`UPDATE dmz_space_label SET
|
||||
c_name=:name, c_color=:color, c_revised=:revised
|
||||
WHERE c_orgid=:orgid AND c_refid=:refid`),
|
||||
l)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute update label")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Delete removes space label from the store.
|
||||
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
|
||||
return s.DeleteConstrained(ctx.Transaction, "dmz_space_label", ctx.OrgID, id)
|
||||
}
|
||||
|
||||
// RemoveReference clears space.labelID for given label.
|
||||
func (s Store) RemoveReference(ctx domain.RequestContext, spaceID string) (err error) {
|
||||
_, err = ctx.Transaction.Exec(s.Bind(`UPDATE dmz_space SET
|
||||
c_labelid='', c_revised=?
|
||||
WHERE c_orgid=? AND c_refid=?`),
|
||||
time.Now().UTC(), ctx.OrgID, spaceID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "execute remove space label reference")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue