mirror of
https://github.com/get-drexa/drive.git
synced 2025-11-30 21:41:39 +00:00
feat: introduce account
This commit is contained in:
23
apps/backend/internal/account/account.go
Normal file
23
apps/backend/internal/account/account.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
bun.BaseModel `bun:"accounts"`
|
||||
|
||||
ID uuid.UUID `bun:",pk,type:uuid" json:"id"`
|
||||
UserID uuid.UUID `bun:"user_id,notnull,type:uuid" json:"userId"`
|
||||
StorageUsageBytes int64 `bun:"storage_usage_bytes,notnull" json:"storageUsageBytes"`
|
||||
StorageQuotaBytes int64 `bun:"storage_quota_bytes,notnull" json:"storageQuotaBytes"`
|
||||
CreatedAt time.Time `bun:"created_at,notnull" json:"createdAt"`
|
||||
UpdatedAt time.Time `bun:"updated_at,notnull" json:"updatedAt"`
|
||||
}
|
||||
|
||||
func newAccountID() (uuid.UUID, error) {
|
||||
return uuid.NewV7()
|
||||
}
|
||||
8
apps/backend/internal/account/err.go
Normal file
8
apps/backend/internal/account/err.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package account
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrAccountNotFound = errors.New("account not found")
|
||||
ErrAccountAlreadyExists = errors.New("account already exists")
|
||||
)
|
||||
131
apps/backend/internal/account/http.go
Normal file
131
apps/backend/internal/account/http.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/auth"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type HTTPHandler struct {
|
||||
accountService *Service
|
||||
authService *auth.Service
|
||||
db *bun.DB
|
||||
authMiddleware fiber.Handler
|
||||
}
|
||||
|
||||
type registerAccountRequest struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
DisplayName string `json:"displayName"`
|
||||
}
|
||||
|
||||
type registerAccountResponse struct {
|
||||
Account *Account `json:"account"`
|
||||
User *user.User `json:"user"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
}
|
||||
|
||||
const currentAccountKey = "currentAccount"
|
||||
|
||||
func CurrentAccount(c *fiber.Ctx) *Account {
|
||||
return c.Locals(currentAccountKey).(*Account)
|
||||
}
|
||||
|
||||
func NewHTTPHandler(accountService *Service, authService *auth.Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
|
||||
return &HTTPHandler{accountService: accountService, authService: authService, db: db, authMiddleware: authMiddleware}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) fiber.Router {
|
||||
api.Post("/accounts", h.registerAccount)
|
||||
|
||||
account := api.Group("/accounts/:accountID")
|
||||
account.Use(h.authMiddleware)
|
||||
account.Use(h.accountMiddleware)
|
||||
|
||||
account.Get("/", h.getAccount)
|
||||
|
||||
return account
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) accountMiddleware(c *fiber.Ctx) error {
|
||||
user, err := auth.AuthenticatedUser(c)
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
accountID, err := uuid.Parse(c.Params("accountID"))
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
account, err := h.accountService.AccountByID(c.Context(), h.db, user.ID, accountID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrAccountNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
c.Locals(currentAccountKey, account)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) getAccount(c *fiber.Ctx) error {
|
||||
account := CurrentAccount(c)
|
||||
if account == nil {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
return c.JSON(account)
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) registerAccount(c *fiber.Ctx) error {
|
||||
req := new(registerAccountRequest)
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
}
|
||||
|
||||
tx, err := h.db.BeginTx(c.Context(), nil)
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
acc, u, err := h.accountService.Register(c.Context(), tx, RegisterOptions{
|
||||
Email: req.Email,
|
||||
Password: req.Password,
|
||||
DisplayName: req.DisplayName,
|
||||
})
|
||||
if err != nil {
|
||||
var ae *user.AlreadyExistsError
|
||||
if errors.As(err, &ae) {
|
||||
return c.SendStatus(fiber.StatusConflict)
|
||||
}
|
||||
if errors.Is(err, ErrAccountAlreadyExists) {
|
||||
return c.SendStatus(fiber.StatusConflict)
|
||||
}
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
}
|
||||
|
||||
result, err := h.authService.GenerateTokenForUser(c.Context(), tx, u)
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return c.JSON(registerAccountResponse{
|
||||
Account: acc,
|
||||
User: u,
|
||||
AccessToken: result.AccessToken,
|
||||
RefreshToken: result.RefreshToken,
|
||||
})
|
||||
}
|
||||
107
apps/backend/internal/account/service.go
Normal file
107
apps/backend/internal/account/service.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/database"
|
||||
"github.com/get-drexa/drexa/internal/password"
|
||||
"github.com/get-drexa/drexa/internal/user"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
userService user.Service
|
||||
}
|
||||
|
||||
type RegisterOptions struct {
|
||||
Email string
|
||||
Password string
|
||||
DisplayName string
|
||||
}
|
||||
|
||||
type CreateAccountOptions struct {
|
||||
OrganizationID uuid.UUID
|
||||
QuotaBytes int64
|
||||
}
|
||||
|
||||
func NewService(userService *user.Service) *Service {
|
||||
return &Service{
|
||||
userService: *userService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Register(ctx context.Context, db bun.IDB, opts RegisterOptions) (*Account, *user.User, error) {
|
||||
hashed, err := password.Hash(opts.Password)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
u, err := s.userService.RegisterUser(ctx, db, user.UserRegistrationOptions{
|
||||
Email: opts.Email,
|
||||
Password: hashed,
|
||||
DisplayName: opts.DisplayName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
acc, err := s.CreateAccount(ctx, db, u.ID, CreateAccountOptions{
|
||||
// TODO: make quota configurable
|
||||
QuotaBytes: 1024 * 1024 * 1024, // 1GB
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return acc, u, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateAccount(ctx context.Context, db bun.IDB, userID uuid.UUID, opts CreateAccountOptions) (*Account, error) {
|
||||
id, err := newAccountID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
account := &Account{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
StorageQuotaBytes: opts.QuotaBytes,
|
||||
}
|
||||
|
||||
_, err = db.NewInsert().Model(account).Returning("*").Exec(ctx)
|
||||
if err != nil {
|
||||
if database.IsUniqueViolation(err) {
|
||||
return nil, ErrAccountAlreadyExists
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (s *Service) AccountByUserID(ctx context.Context, db bun.IDB, userID uuid.UUID) (*Account, error) {
|
||||
var account Account
|
||||
err := db.NewSelect().Model(&account).Where("user_id = ?", userID).Scan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAccountNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &account, nil
|
||||
}
|
||||
|
||||
func (s *Service) AccountByID(ctx context.Context, db bun.IDB, userID uuid.UUID, id uuid.UUID) (*Account, error) {
|
||||
var account Account
|
||||
err := db.NewSelect().Model(&account).Where("user_id = ?", userID).Where("id = ?", id).Scan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAccountNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &account, nil
|
||||
}
|
||||
Reference in New Issue
Block a user