Example #1
0
// LoginHandler user login
func LoginHandler(c *gin.Context) {
	type Login struct {
		Email    string `json:"email" binding:"required"`
		Password string `json:"password" binding:"required"`
	}

	var payload Login

	err := c.BindJSON(&payload)
	if err != nil {
		x.Panic("USER_JSON_BIND_ERROR", err.Error())

		return
	}

	auth, err := service.Login(payload.Email, payload.Password)
	if err != nil {
		x.Error("USER_EMAIL_LOGIN_ERROR", err.Error())

		return
	}

	if err == nil {
		x.Output(auth)

		return
	}

	x.Error("INVALID_AUTH", "unauthorize user")
}
Example #2
0
// RegisterHandler user register
func RegisterHandler(c *gin.Context) {
	var payload Object

	err := c.BindJSON(&payload)
	if err != nil {
		x.Panic("JSON_BIND_ERROR", err.Error())

		return
	}

	user, err := service.Register(payload)
	if err != nil {
		x.Error("USER_EMAIL_REGISTER_ERROR", err.Error())

		return
	}

	// success
	x.Output(user)
}
Example #3
0
// Handler test
func Handler(c *gin.Context) {
	id := c.Param("id")
	switch c.Request.Method {
	case x.GET:
		// list
		if id == "" {
			filter := Object{
				Title:       c.Query("filter.title"),
				Description: c.Query("filter.description"),
			}

			option := Option{
				Slice:  c.Query("slice"),
				Order:  c.Query("order"),
				Filter: filter,
			}

			d, err := service.Find(option)
			if err != nil {
				x.Error("GET_RESOURCE_"+strings.ToUpper(resourceName), err.Error())
			}

			x.Output(d)

			return
		}

		// detail
		d, err := service.Get(id)
		if err != nil {
			x.Error("GET_RESOURCE_"+strings.ToUpper(resourceName), err.Error())

			return
		}

		x.Output(d)

		return
	case x.POST:
		var payload Object
		err := c.BindJSON(&payload)
		if err != nil {
			x.Panic("REQUIRED_FIELDS", "field is required")

			return
		}

		d, err := service.Create(payload)
		if err != nil {
			x.Error("POST_RESOURCE_"+strings.ToUpper(resourceName), err.Error())
		}

		x.Output(d)

		return
	case x.PATCH:
		if id == "" {
			x.Error("RESOURCE_ID_REQUIRED", "resource id is missing")

			return
		}

		var payload Object
		err := c.BindJSON(&payload)
		if err != nil {
			x.Panic("REQUIRED_FIELDS", "field is required")

			return
		}

		d, err := service.Update(payload, id)
		if err != nil {
			x.Error("PUT_RESOURCE_"+strings.ToUpper(resourceName), err.Error())

			return
		}

		x.Output(d)

		return
	case x.DELETE:
		if id == "" {
			x.Error("RESOURCE_ID_REQUIRED", "resource id is missing")

			return
		}

		d, err := service.Remove(id)
		if err != nil {
			x.Error("DELETE_RESOURCE_"+strings.ToUpper(resourceName), err.Error())

			return
		}

		x.Output(d)

		return
	}

	x.MethodNotAllowedError()
}