Commit 288aec38 authored by zauberstuhl's avatar zauberstuhl

Implement ApiAspect

parent e292ecd1
package controllers
//
// GangGo API Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"github.com/revel/revel"
"gopkg.in/ganggo/ganggo.v0/app/models"
"strconv"
)
type ApiAspect struct {
*revel.Controller
}
func (a ApiAspect) ShowPerson() revel.Result {
var (
personID uint
aspects models.Aspects
)
a.Params.Bind(&personID, "id")
user, err := models.GetCurrentUser(a.Session["TOKEN"])
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
err = aspects.FindByUserPersonID(user.ID, personID)
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
return a.RenderJson(aspects)
}
func (a ApiAspect) CreatePerson() revel.Result {
var personID, aspectID uint
a.Params.Bind(&personID, "id")
a.Params.Bind(&aspectID, "aspect_id")
membership := models.AspectMembership{
AspectID: aspectID,
PersonID: personID,
}
err := membership.Create()
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
return a.RenderJson(membership)
}
func (a ApiAspect) DeletePerson() revel.Result {
var personID, aspectID uint
a.Params.Bind(&personID, "id")
a.Params.Bind(&aspectID, "aspect_id")
membership := models.AspectMembership{
AspectID: aspectID,
PersonID: personID,
}
err := membership.Delete()
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
return a.RenderJson(membership)
}
func (a ApiAspect) Index() revel.Result {
user, err := models.GetCurrentUser(a.Session["TOKEN"])
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
return a.RenderJson(user.Aspects)
}
func (a ApiAspect) Create() revel.Result {
name := a.Params.Get("name")
personID, err := strconv.ParseUint(
a.Params.Get("personId"), 0, 64)
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
revel.TRACE.Println("params", name, personID)
user, err := models.GetCurrentUser(a.Session["TOKEN"])
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
var person models.Person
err = person.FindByID(uint(personID))
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
aspect := models.Aspect{
Name: name,
UserID: user.ID,
Memberships: []models.AspectMembership{
models.AspectMembership{
PersonID: person.ID,
},
},
}
revel.TRACE.Println("aspect", aspect)
err = aspect.Create()
if err != nil {
revel.ERROR.Println(err)
return a.RenderError(err)
}
return a.RenderJson(aspect)
}
func (a ApiAspect) Show() revel.Result {
return a.NotFound("Not implemented yet")
}
func (a ApiAspect) Delete() revel.Result {
return a.NotFound("Not implemented yet")
}
......@@ -23,3 +23,11 @@ DELETE /api/v0/people/:id ApiPeople.Delete
GET /api/v0/people/:id/profile ApiProfile.Show
POST /api/v0/people/:id/profile ApiProfile.Create
DELETE /api/v0/people/:id/profile ApiProfile.Delete
GET /api/v0/aspects ApiAspect.Index
POST /api/v0/aspects ApiAspect.Create
GET /api/v0/aspects/:id ApiAspect.Show
DELETE /api/v0/aspects/:id ApiAspect.Delete
GET /api/v0/people/:id/aspects ApiAspect.ShowPerson
POST /api/v0/people/:id/aspects/:aspect_id ApiAspect.CreatePerson
DELETE /api/v0/people/:id/aspects/:aspect_id ApiAspect.DeletePerson
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment