Skip to content

Commit

Permalink
remove endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Jan 4, 2024
1 parent f65a00e commit 38ae9b3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/spec/components/schemas/Template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ allOf:
- is_completed
- template_name
- template_short_name
- template-id
properties:
template:
type: object
Expand All @@ -33,4 +34,7 @@ allOf:
type: string
template_short_name:
type: string
template_id:
type: int
format: int64

16 changes: 16 additions & 0 deletions internal/data/pg/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ func NewTemplateQ(db *pgdb.DB) data.TemplateQ {
db: db,
sql: sq.Select("b.*").From(fmt.Sprintf("%s as b", templateTableName)),
upd: sq.Update(templateTableName),
dlt: sq.Delete(templateTableName),
}
}

type TemplateQ struct {
db *pgdb.DB
sql sq.SelectBuilder
upd sq.UpdateBuilder
dlt sq.DeleteBuilder
}

func (q *TemplateQ) New() data.TemplateQ {
Expand Down Expand Up @@ -79,6 +81,15 @@ func (q *TemplateQ) Insert(value *data.Template) error {
return nil
}

func (q *TemplateQ) Delete() error {
err := q.db.Exec(q.dlt)
if err != nil {
return errors.Wrap(err, "failed to delete template")
}

return nil
}

func (q *TemplateQ) Page(pageParams pgdb.OffsetPageParams) data.TemplateQ {
q.sql = pageParams.ApplyTo(q.sql, idField)

Expand All @@ -87,27 +98,32 @@ func (q *TemplateQ) Page(pageParams pgdb.OffsetPageParams) data.TemplateQ {

func (q *TemplateQ) FilterByUser(id int64) data.TemplateQ {
q.sql = q.sql.Where(sq.Eq{userIDField: id})
q.upd = q.upd.Where(sq.Eq{userIDField: id})
q.dlt = q.dlt.Where(sq.Eq{userIDField: id})

return q
}

func (q *TemplateQ) FilterByID(id int64) data.TemplateQ {
q.sql = q.sql.Where(sq.Eq{idField: id})
q.upd = q.upd.Where(sq.Eq{idField: id})
q.dlt = q.dlt.Where(sq.Eq{idField: id})

return q
}

func (q *TemplateQ) FilterByName(name string) data.TemplateQ {
q.sql = q.sql.Where(sq.Eq{nameField: name})
q.upd = q.upd.Where(sq.Eq{nameField: name})
q.dlt = q.dlt.Where(sq.Eq{nameField: name})

return q
}

func (q *TemplateQ) FilterByShortName(name string) data.TemplateQ {
q.sql = q.sql.Where(sq.Eq{nameField: name})
q.upd = q.upd.Where(sq.Eq{nameField: name})
q.dlt = q.dlt.Where(sq.Eq{nameField: name})

return q
}
14 changes: 8 additions & 6 deletions internal/data/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type TemplateQ interface {
Get() (*Template, error)
Insert(data *Template) error
Update(data *Template) error
Delete() error
Select() ([]Template, error)
FilterByUser(ids int64) TemplateQ
FilterByName(name string) TemplateQ
Expand All @@ -13,10 +14,11 @@ type TemplateQ interface {
}

type Template struct {
ID int64 `db:"id" structs:"-"`
UserID int64 `db:"user_id" structs:"user_id"`
Name string `db:"name" structs:"name"`
ShortName string `db:"short_name" structs:"short_name"`
Template []byte `db:"template" structs:"template"`
ImgBytes []byte `db:"img_bytes" structs:"img_bytes"` //todo make better
ID int64 `db:"id" structs:"-"`
UserID int64 `db:"user_id" structs:"user_id"`
Name string `db:"name" structs:"name"`
ShortName string `db:"short_name" structs:"short_name"`
IsDefaultTemplate bool `db:"is_default_template" structs:"is_default_template"`
Template []byte `db:"template" structs:"template"`
ImgBytes []byte `db:"img_bytes" structs:"img_bytes"` //todo make better
}
1 change: 1 addition & 0 deletions internal/service/api/handlers/create_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func CreateTemplate(w http.ResponseWriter, r *http.Request) {
Name: req.Data.Attributes.TemplateName,
ShortName: req.Data.Attributes.TemplateShortName,
UserID: client.ID,
//IsDefaultTemplate: TODO update
})
if err != nil {
Log(r).WithError(err).Error("failed to insert template")
Expand Down
2 changes: 2 additions & 0 deletions internal/service/api/handlers/get_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func GetTemplates(w http.ResponseWriter, r *http.Request) {
ape.RenderErr(w, problems.InternalError())
return
}

ape.Render(w, newTemlateListResp(tmps))

}
Expand All @@ -47,6 +48,7 @@ func newTemlateListResp(tmps []data.Template) resources.TemplateListResponse {
Attributes: resources.TemplateAttributes{
BackgroundImg: string(tmp.ImgBytes),
TemplateName: tmp.Name,
TemplateId: tmp.ID,
},
})
}
Expand Down
27 changes: 27 additions & 0 deletions internal/service/api/handlers/remove_template_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handlers

import (
"gitlab.com/distributed_lab/ape"
"gitlab.com/distributed_lab/ape/problems"
"gitlab.com/tokend/course-certificates/ccp/internal/service/api/requests"
"net/http"
)

func RemoveTemplateByID(w http.ResponseWriter, r *http.Request) {
templateID, err := requests.NewRemoveTemplateByIDRequest(r)
if err != nil {
Log(r).WithError(err).Error("failed to parse request ")
ape.Render(w, problems.BadRequest(err))
return
}

templateID64 := int64(*templateID)
err = MasterQ(r).TemplateQ().FilterByID(templateID64).Delete()
if err != nil {
Log(r).WithError(err).Error("failed to remove template")
ape.Render(w, problems.InternalError())
return
}

w.WriteHeader(http.StatusOK)
}
32 changes: 32 additions & 0 deletions internal/service/api/requests/remove_template_by_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/go-chi/chi"
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/distributed_lab/urlval"
"net/http"
"strconv"
)

const (
TemplateIDPathParam = "template_id"
)

type RemoveTemplateByIDRequest struct {
TemplateID string `url:"-"`
}

func NewRemoveTemplateByIDRequest(r *http.Request) (*int, error) {
request := RemoveTemplateByIDRequest{}
if err := urlval.Decode(r.URL.Query(), &request); err != nil {
return nil, errors.Wrap(err, "failed to decode data")
}
request.TemplateID = chi.URLParam(r, TemplateIDPathParam)

id, err := strconv.Atoi(request.TemplateID)
if err != nil {
return nil, errors.Wrap(err, "failed to convert template id to int")
}

return &id, nil
}
1 change: 1 addition & 0 deletions internal/service/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (s *service) router(cfg config.Config) chi.Router {
r.Route("/template", func(r chi.Router) {
r.Post("/", handlers.CreateTemplate)
r.Get("/{user}", handlers.GetTemplates)
r.Delete("/{template_id}", handlers.RemoveTemplateByID)
r.Get("/{user}/{name}", handlers.GetTemplateByName)
})
})
Expand Down
1 change: 1 addition & 0 deletions resources/model_template_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type TemplateAttributes struct {
BackgroundImg string `json:"background_img"`
IsCompleted bool `json:"is_completed"`
Template json.RawMessage `json:"template"`
TemplateId int64 `json:"template_id,omitempty"`
TemplateName string `json:"template_name"`
TemplateShortName string `json:"template_short_name"`
}

0 comments on commit 38ae9b3

Please sign in to comment.