Commit 8584cb2f authored by Lukas Matt's avatar Lukas Matt

Implement custom user stream call

added creation and deletion of a UserStream struct
parent 278c3908
......@@ -28,4 +28,5 @@ func init() {
revel.InterceptMethod((*ApiAspect).checkUser, revel.BEFORE)
revel.InterceptMethod((*ApiNotification).checkUser, revel.BEFORE)
revel.InterceptMethod((*ApiOAuth).checkUser, revel.BEFORE)
revel.InterceptMethod((*ApiUserStream).checkUser, revel.BEFORE)
}
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 (
"net/http"
"gopkg.in/ganggo/ganggo.v0/app/models"
"gopkg.in/ganggo/api.v0/app/helpers"
"github.com/revel/revel"
)
/**
* @apiDefine UserStreams UserStreams endpoint
*
* Viewing, creating and deleting UserStream structures
*/
type ApiUserStream struct {
*revel.Controller
ApiHelper
}
/**
* @api {post} /users/streams Create a new user stream
* @apiName ApiUserStream.Create
* @apiGroup UserStreams
*
* @apiParam {String} access_token Oauth access token
* @apiParam {String} name The name of stream
* @apiParam {String} [tags] Tags separated by ","
* @apiParam {String} [people] Author handle (e.g. lukas@sechat.org) separated by ","
* @apiParam {String} [expression] Regular expression
*
* @apiSuccess {Number} UserID User database identifier
* @apiSuccess {String} Name User stream name
* @apiSuccess {String} Tags Tags separated by ","
* @apiSuccess {String} People People separated by ","
* @apiSuccess {String} Expression Regular Expression
*
* @apiSuccessExample {json} Success-Response
* HTTP/1.1 200 OK
* {
* "UserID": 1,
* "Name": "Test",
* "Tags": "linux,software,opensource",
* "People": "lukas@sechat.org,hq@ggg.social",
* "Expression": "ganggo|socialhome"
* }
*
* @apiError (Errors) {String} error Contains the recent error message
*
* @apiErrorExample {json} BadRequest
* HTTP/1.1 400 Bad Request
* {
* "error": "[...]"
* }
*
* @apiErrorExample {json} Unauthorized
* HTTP/1.1 401 Unauthorized
* {
* "error": "[...]"
* }
*
* @apiErrorExample {json} ServerError
* HTTP/1.1 500 Internal Server Error
* {
* "error": "[...]"
* }
*
*/
func (s ApiUserStream) Create(name, tags, people, expression string) revel.Result {
if helpers.EmptyString(name) {
s.Log.Error(TAG, "api", ERR_EMPTY_STRING)
return s.ApiError(http.StatusBadRequest, ERR_EMPTY_STRING)
}
stream := models.UserStream{
UserID: s.CurrentUser.ID,
Name: name,
Tags: tags,
People: people,
Expression: expression,
}
err := stream.Create()
if err != nil {
s.Log.Error(TAG, "db", err, "api", ERR_SERVER)
return s.ApiError(http.StatusInternalServerError, ERR_SERVER)
}
return s.RenderJSON(stream)
}
/**
* @api {delete} /users/streams/:id Delete a user stream
* @apiName ApiUserStream.Delete
* @apiGroup UserStreams
*
* @apiParam {String} access_token Oauth access token
* @apiParam {Number} id The database identifier for the struct
*
* @apiSuccessExample {json} Success-Response
* HTTP/1.1 200 OK
* {}
*
* @apiError (Errors) {String} error Contains the recent error message
*
* @apiErrorExample {json} BadRequest
* HTTP/1.1 400 Bad Request
* {
* "error": "[...]"
* }
*
* @apiErrorExample {json} Unauthorized
* HTTP/1.1 401 Unauthorized
* {
* "error": "[...]"
* }
*
*/
func (s ApiUserStream) Delete(id uint) revel.Result {
userStream := models.UserStream{
ID: id,
UserID: s.CurrentUser.ID,
}
err := userStream.Delete()
if err != nil {
s.Log.Error(TAG, "db", err, "api", ERR_BAD_REQUEST)
return s.ApiError(http.StatusBadRequest, ERR_BAD_REQUEST)
}
return s.RenderJSON(struct{}{})
}
......@@ -10,6 +10,9 @@ GET /api/v0/posts/:guid ApiPost.Show
DELETE /api/v0/posts/:guid ApiPost.Delete
POST /api/v0/posts/:guid/reshare ApiPost.Reshare
POST /api/v0/users/streams ApiUserStream.Create
DELETE /api/v0/users/streams/:id ApiUserStream.Delete
GET /api/v0/notifications ApiNotification.Index
PUT /api/v0/notifications/:id ApiNotification.Update
......
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