101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"main/model"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type createUserRequest struct {
|
|
Username string `json:"username" binding:"required,alphanum"`
|
|
Password string `json:"password" binding:"required,min=6"`
|
|
FullName string `json:"full_name" binding:"required"`
|
|
Email string `json:"email" binding:"required,email"`
|
|
}
|
|
|
|
type userResponse struct {
|
|
Username string `json:"username"`
|
|
FullName string `json:"full_name"`
|
|
Email string `json:"email"`
|
|
PasswordChangedAt time.Time `json:"password_changed_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (server *Server) listUsers(ctx *gin.Context) {
|
|
users, err := model.ListUsers()
|
|
if err != nil {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list users"})
|
|
return
|
|
}
|
|
ctx.JSON(http.StatusOK, users)
|
|
}
|
|
|
|
func (server *Server) createUser(ctx *gin.Context) {
|
|
var req createUserRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
ctx.JSON(http.StatusBadRequest, errorResponse(err))
|
|
return
|
|
}
|
|
|
|
rsp := ""
|
|
ctx.JSON(http.StatusOK, rsp)
|
|
}
|
|
|
|
type loginUserRequest struct {
|
|
Username string `json:"username" binding:"required,alphanum"`
|
|
Password string `json:"password" binding:"required,min=6"`
|
|
}
|
|
|
|
type loginUserResponse struct {
|
|
SessionID string `json:"session_id"`
|
|
AccessToken string `json:"access_token"`
|
|
AccessTokenExpiresAt time.Time `json:"access_token_expires_at"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
RefreshTokenExpiresAt time.Time `json:"refresh_token_expires_at"`
|
|
User userResponse `json:"user"`
|
|
}
|
|
|
|
type Account struct {
|
|
ID int64 `json:"id"`
|
|
Owner string `json:"owner"`
|
|
Balance int64 `json:"balance"`
|
|
Currency string `json:"currency"`
|
|
}
|
|
|
|
func (server *Server) welcome(ctx *gin.Context) {
|
|
ctx.JSON(http.StatusOK, "welcome")
|
|
}
|
|
|
|
func (server *Server) loginUser(ctx *gin.Context) {
|
|
ctx.JSON(http.StatusOK, "loginUser")
|
|
}
|
|
|
|
func (server *Server) createAccount(ctx *gin.Context) {
|
|
|
|
}
|
|
|
|
func (server *Server) getAccount(ctx *gin.Context) {
|
|
|
|
}
|
|
|
|
func (server *Server) listAccounts(ctx *gin.Context) {
|
|
account1 := &Account{
|
|
1,
|
|
"2",
|
|
3,
|
|
"4",
|
|
}
|
|
account2 := &Account{
|
|
11,
|
|
"22",
|
|
33,
|
|
"44",
|
|
}
|
|
var accounts []*Account
|
|
accounts = append(accounts, account1)
|
|
accounts = append(accounts, account2)
|
|
|
|
ctx.JSON(http.StatusOK, accounts)
|
|
}
|