Beispiel #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")
}
Beispiel #2
0
// UploadHandler file
func UploadHandler(c *gin.Context) {
	os.Mkdir(uploadPath, 0777)

	file, header, err := c.Request.FormFile(uploadField)
	if err != nil {
		x.Error("FILE_UPLOAD_ERROR", err.Error())

		return
	}

	ext := getExtension(header.Filename)
	name := x.GenerateHash() + "." + ext
	filePath := uploadPath + name
	out, err := os.Create(filePath)
	if err != nil {
		x.Error("FILE_UPLOAD_CREATE_ERROR", err.Error())

		return
	}

	defer out.Close()
	_, err = io.Copy(out, file)
	if err != nil {
		x.Error("FILE_UPLOAD_COPY_ERROR", err.Error())

		return
	}

	size, err := getFileSize(file)
	if err != nil {
		x.Error("FILE_UPLOAD_SIZE_ERROR", err.Error())

		return
	}

	// check mime type
	mime := header.Header.Get("Content-Type")
	if mime == "" {
		x.Error("FILE_UPLOAD_MIME_ERROR", "cant get file content-type")

		return
	}

	// save meta on database
	payload := Object{
		ID:   name,
		Ext:  ext,
		Size: size,
		Mime: mime,
	}

	service.Create(payload)

	x.Output(payload)
}
Beispiel #3
0
// MeHandler check authentication
func MeHandler(c *gin.Context) {
	user, err := service.Get(x.GetAuthUser().ID)
	if err != nil {
		x.Error("INVALID_USER", "no user found with this token")

		return
	}

	x.Output(user)

	return
}
Beispiel #4
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)
}
Beispiel #5
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()
}