controllers

package
v8.5.4+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2017 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UserController

type UserController struct {
	// mvc.C is just a lightweight lightweight alternative
	// to the "mvc.Controller" controller type,
	// use it when you don't need mvc.Controller's fields
	// (you don't need those fields when you return values from the method functions).
	mvc.C

	// Our UserService, it's an interface which
	// is binded from the main application.
	Service services.UserService

	// Session-relative things.
	Manager *sessions.Sessions
	Session *sessions.Session
}

UserController is our /user controller. UserController is responsible to handle the following requests: GET /user/register POST /user/register GET /user/login POST /user/login GET /user/me All HTTP Methods /user/logout

func (*UserController) AnyLogout

func (c *UserController) AnyLogout()

AnyLogout handles All/Any HTTP Methods for: http://localhost:8080/user/logout.

func (*UserController) BeginRequest

func (c *UserController) BeginRequest(ctx context.Context)

BeginRequest will set the current session to the controller.

Remember: iris.Context and context.Context is exactly the same thing, iris.Context is just a type alias for go 1.9 users. We use context.Context here because we don't need all iris' root functions, when we see the import paths, we make it visible to ourselves that this file is using only the context.

func (*UserController) GetLogin

func (c *UserController) GetLogin() mvc.Result

GetLogin handles GET: http://localhost:8080/user/login.

func (*UserController) GetMe

func (c *UserController) GetMe() mvc.Result

GetMe handles GET: http://localhost:8080/user/me.

func (*UserController) GetRegister

func (c *UserController) GetRegister() mvc.Result

GetRegister handles GET: http://localhost:8080/user/register.

func (*UserController) PostLogin

func (c *UserController) PostLogin() mvc.Result

PostLogin handles POST: http://localhost:8080/user/register.

func (*UserController) PostRegister

func (c *UserController) PostRegister() mvc.Result

PostRegister handles POST: http://localhost:8080/user/register.

type UsersController

type UsersController struct {
	mvc.C

	Service services.UserService
}

UsersController is our /users API controller. GET /users | get all GET /users/{id:long} | get by id PUT /users/{id:long} | update by id DELETE /users/{id:long} | delete by id Requires basic authentication.

func (*UsersController) DeleteBy

func (c *UsersController) DeleteBy(id int64) interface{}

DeleteBy deletes a user. Demo: curl -i -X DELETE -u admin:password http://localhost:8080/users/1

func (*UsersController) Get

func (c *UsersController) Get() (results []datamodels.User)

Get returns list of the users. Demo: curl -i -u admin:password http://localhost:8080/users

The correct way if you have sensitive data:

func (c *UsersController) Get() (results []viewmodels.User) {
	data := c.Service.GetAll()

	for _, user := range data {
		results = append(results, viewmodels.User{user})
	}
	return
}

otherwise just return the datamodels.

func (*UsersController) GetBy

func (c *UsersController) GetBy(id int64) (user datamodels.User, found bool)

GetBy returns a user. Demo: curl -i -u admin:password http://localhost:8080/users/1

func (*UsersController) PutBy

func (c *UsersController) PutBy(id int64) (datamodels.User, error)

PutBy updates a user. Demo: curl -i -X PUT -u admin:password -F "username=kataras" -F "password=rawPasswordIsNotSafeIfOrNotHTTPs_You_Should_Use_A_client_side_lib_for_hash_as_well" http://localhost:8080/users/1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL