busybar / internal / controllers / user.contoller.go
user.contoller.go
Raw
package controllers

import (
	"net/http"

	"github.com/danielrhuynh/busybar/internal/auth"
	"github.com/danielrhuynh/busybar/internal/models"
	"github.com/danielrhuynh/busybar/internal/services"

	"github.com/labstack/echo/v4"
)

func UserRegister(c echo.Context) error {
	userSession, err := auth.GetUser(c)
	if err != nil {
		return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
	}

	req, err := getInput[models.RegistrationRequest](c)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}

	_, err = services.UpdateUser(userSession.ID, *req)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}

	return c.JSON(http.StatusAccepted, envelope{
		"message": "User updated",
	})
}

func UserDeleteAccount(c echo.Context) error {
	userSession, err := auth.GetUser(c)
	if err != nil {
		return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
	}

	err = services.DeleteAccount(userSession.ID)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}

	return c.JSON(http.StatusAccepted, envelope{
		"message": "Account deleted",
	})
}