package account

import (
	"log"

	"git.trj.tw/golang/go-gallery/models"
	"git.trj.tw/golang/go-gallery/modules/context"
	"git.trj.tw/golang/go-gallery/modules/utils"
)

// UserLogin route
func UserLogin(c *context.Context) {
	loginArg := struct {
		Account  string `form:"account" json:"account" binding:"required"`
		Password string `form:"password" json:"password" binding:"required"`
	}{
		Account:  "",
		Password: "",
	}
	err := c.ShouldBind(&loginArg)
	if err != nil {
		c.DataFormat(nil)
		return
	}

	acc, err := models.GetAccount(loginArg.Account)

	if err != nil {
		log.Println(err)
		c.ServerError(nil)
		return
	}
	if acc == nil {
		c.NotFound("User not found")
		return
	}

	c.Success(utils.ToMap(acc))
}
