Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:go-park-mail-ru/2023_2_Hamster i…
Browse files Browse the repository at this point in the history
…nto deploy
  • Loading branch information
DmitriyKomarovCoder committed Nov 24, 2023
2 parents 9b20c21 + 336cc47 commit e6371d7
Show file tree
Hide file tree
Showing 25 changed files with 1,128 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ db: ## Connect to the database
cover:
sh scripts/coverage_test.sh

lint: ## Run linters
#lint: ## Run linters
golangci-lint run

test: ## Run tests
Expand Down
57 changes: 31 additions & 26 deletions build/schema/initdb.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE Users
(
CREATE TABLE IF NOT EXISTS Users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
username VARCHAR(20) NOT NULL,
login VARCHAR(20) UNIQUE NOT NULL,
Expand All @@ -10,11 +9,18 @@ CREATE TABLE Users
avatar_url UUID
);

CREATE TABLE Accounts (
CREATE TABLE IF NOT EXISTS Accounts (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES Users(id),
balance numeric(10, 2),
mean_payment TEXT
accumulation BOOLEAN,
balance_enabled BOOLEAN,
mean_payment VARCHAR(30)
);

CREATE TABLE IF NOT EXISTS UserAccount (
user_id UUID REFERENCES Users(id),
account_id UUID REFERENCES Accounts(id),
PRIMARY KEY (user_id, account_id)
);

CREATE TABLE IF NOT EXISTS category (
Expand All @@ -28,7 +34,7 @@ CREATE TABLE IF NOT EXISTS category (
);


CREATE TABLE Transaction (
CREATE TABLE IF NOT EXISTS Transaction (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES Users(id),
account_income UUID REFERENCES Accounts(id),
Expand All @@ -40,19 +46,22 @@ CREATE TABLE Transaction (
description VARCHAR(100)
);

CREATE TABLE TransactionCategory (
CREATE TABLE IF NOT EXISTS TransactionCategory (
transaction_id UUID REFERENCES Transaction(id),
category_id UUID REFERENCES Category(id),
PRIMARY KEY (transaction_id, category_id)
);

--========================================================================

CREATE OR REPLACE FUNCTION add_default_categories_accounts_transactions()
RETURNS TRIGGER AS $$
DECLARE
categoryID UUID;
transaction_idI UUID;
transaction_idO UUID;
accountID UUID;
accountCashID UUID;
accountCardID UUID;
BEGIN
INSERT INTO category (user_id, parent_tag, "name", show_income, show_outcome, regular)
VALUES (NEW.id, NULL, 'Дети', false, true, false),
Expand All @@ -71,19 +80,25 @@ BEGIN

SELECT id INTO categoryID FROM category WHERE name = 'Продукты' AND user_id = NEW.id;

INSERT INTO accounts(user_id, balance, mean_payment)
VALUES (NEW.id, 0, 'Карта');
INSERT INTO accounts(balance, mean_payment, accumulation, balance_enabled)
VALUES (0, 'Карта', false, true) RETURNING id INTO accountCardID;

INSERT INTO accounts(user_id, balance, mean_payment)
VALUES (NEW.id, 0, 'Наличка') RETURNING id INTO accountID;
INSERT INTO accounts(balance, mean_payment, accumulation, balance_enabled)
VALUES (0, 'Наличка', false, true) RETURNING id INTO accountCashID;

INSERT INTO userAccount(user_id, account_id)
VALUES (NEW.id, accountCardID);

INSERT INTO userAccount(user_id, account_id)
VALUES (NEW.id, accountCashID);

INSERT INTO transaction(user_id, account_income, account_outcome, income, outcome, payer, description)
VALUES (NEW.id, accountID,
accountID, 100, 0, 'Пятерочка', 'Пошел в магазин за вкусняшками') RETURNING id INTO transaction_idI;
VALUES (NEW.id, accountCardID,
accountCardID, 100, 0, 'Пятерочка', 'Пошел в магазин за вкусняшками') RETURNING id INTO transaction_idI;

INSERT INTO transaction(user_id, account_income, account_outcome, income, outcome, payer, description)
VALUES (NEW.id, accountID,
accountID, 0, 100, 'Пятерочка', 'Вернули деньги оплата не прошла') RETURNING id INTO transaction_idO;
VALUES (NEW.id, accountCardID,
accountCardID, 0, 100, 'Пятерочка', 'Вернули деньги оплата не прошла') RETURNING id INTO transaction_idO;

INSERT INTO TransactionCategory(transaction_id, category_id)
VALUES (transaction_idI, categoryID),
Expand All @@ -106,13 +121,3 @@ ALTER COLUMN planned_budget SET DEFAULT 0.0;

INSERT INTO "users"(login, username, password_hash, planned_budget)
VALUES ('kossmatof','komarov', '$argon2id$v=19$m=65536,t=1,p=4$m8qhM3XLae+RCTGirBFEww$Znu5RBnxlam2xRoVtwBzbdSrN4/sRCm1IMOVX4N2uxw', 10000);

INSERT INTO "users"(login, username, password_hash, planned_budget)
VALUES ('test','test1', '$argon2id$v=19$m=65536,t=1,p=4$m8qhM3XLae+RCTGirBFEww$Znu5RBnxlam2xRoVtwBzbdSrN4/sRCm1IMOVX4N2uxw', 10000);

INSERT INTO "accounts"(user_id, balance, mean_payment)
VALUES ((SELECT id FROM Users limit 1), 0, 'Кошелек');

INSERT INTO "accounts"(user_id, balance, mean_payment)
VALUES ((SELECT id FROM Users limit 1), 0, 'Наличка');

8 changes: 8 additions & 0 deletions cmd/api/init/app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
userRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/user/repository/postgresql"
userUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/user/usecase"

accountDelivery "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/http"
accountRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/repository/postgresql"
accountUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/usecase"

sessionRep "github.com/go-park-mail-ru/2023_2_Hamster/internal/monolithic/sessions/repository/redis"
sessionUsecase "github.com/go-park-mail-ru/2023_2_Hamster/internal/monolithic/sessions/usecase"

Expand All @@ -40,13 +44,15 @@ func Init(db *pgxpool.Pool, redis *redis.Client, log *logger.Logger) *mux.Router
userRep := userRep.NewRepository(db, *log)
transactionRep := transactionRep.NewRepository(db, *log)
categoryRep := categoryRep.NewRepository(db, *log)
accountRep := accountRep.NewRepository(db, *log)

authUsecase := authUsecase.NewUsecase(authRep, *log)
sessionUsecase := sessionUsecase.NewSessionUsecase(sessionRep)
userUsecase := userUsecase.NewUsecase(userRep, *log)
transactionUsecase := transactionUsecase.NewUsecase(transactionRep, *log)
categoryUsecase := categoryUsecase.NewUsecase(categoryRep, *log)
csrfUsecase := csrfUsecase.NewUsecase(*log)
accountUsecase := accountUsecase.NewUsecase(accountRep, *log)

authMiddlewear := middleware.NewAuthMiddleware(sessionUsecase, userRep, *log)
logMiddlewear := middleware.NewLoggingMiddleware(*log)
Expand All @@ -58,13 +64,15 @@ func Init(db *pgxpool.Pool, redis *redis.Client, log *logger.Logger) *mux.Router
transactionHandler := transactionDelivery.NewHandler(transactionUsecase, *log)
categoryHandler := categoryDelivary.NewHandler(categoryUsecase, *log)
csrfHandler := csrfDelivery.NewHandler(csrfUsecase, *log)
accountHandler := accountDelivery.NewHandler(accountUsecase, *log)

return router.InitRouter(
authHandler,
userHandler,
transactionHandler,
categoryHandler,
csrfHandler,
accountHandler,
logMiddlewear,
recoveryMiddlewear,
authMiddlewear,
Expand Down
16 changes: 13 additions & 3 deletions cmd/api/init/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
_ "github.com/go-park-mail-ru/2023_2_Hamster/docs"
"github.com/prometheus/client_golang/prometheus/promhttp"

account "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/account/delivery/http"
auth "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/auth/delivery/http"
category "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/category/delivery/http"
csrf "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/csrf/delivery/http"
transaction "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/transaction/delivery/http"
user "github.com/go-park-mail-ru/2023_2_Hamster/internal/microservices/user/delivery/http"

"github.com/go-park-mail-ru/2023_2_Hamster/internal/middleware"
"github.com/gorilla/mux"

Expand All @@ -27,6 +27,7 @@ func InitRouter(auth *auth.Handler,
transaction *transaction.Handler,
category *category.Handler,
csrf *csrf.Handler,
account *account.Handler,
logMid *middleware.LoggingMiddleware,
recoveryMid *middleware.RecoveryMiddleware,
authMid *middleware.AuthMiddleware,
Expand All @@ -36,7 +37,7 @@ func InitRouter(auth *auth.Handler,
r.Use(middleware.RequestID)
r.Use(logMid.LoggingMiddleware)
r.Use(recoveryMid.Recoverer)
r.Use(middleware.Timeout(5 * time.Second))
r.Use(middleware.Timeout(1000000 * time.Second))
r.Use(middleware.Heartbeat("ping"))
r.Use(middleware.Metrics())

Expand Down Expand Up @@ -65,6 +66,15 @@ func InitRouter(auth *auth.Handler,
authRouter.Methods("POST").Path("/logout").HandlerFunc(auth.LogOut)
}

accountRouter := apiRouter.PathPrefix("/account").Subrouter()
accountRouter.Use(authMid.Authentication)
accountRouter.Use(csrfMid.CheckCSRF)
{
accountRouter.Methods("POST").Path("/create").HandlerFunc(account.Create)
accountRouter.Methods("PUT").Path("/update").HandlerFunc(account.Update)
accountRouter.Methods("DELETE").Path("/{account_id}/delete").HandlerFunc(account.Delete)
}

userRouter := apiRouter.PathPrefix("/user").Subrouter()
userRouter.Use(authMid.Authentication)
userRouter.Use(csrfMid.CheckCSRF)
Expand Down Expand Up @@ -95,7 +105,7 @@ func InitRouter(auth *auth.Handler,

categoryRouter := apiRouter.PathPrefix("/tag").Subrouter()
categoryRouter.Use(authMid.Authentication)
// categoryRouter.Use(csrfMid.CheckCSRF)
categoryRouter.Use(csrfMid.CheckCSRF)
{
categoryRouter.Methods("POST").Path("/create").HandlerFunc(category.CreateTag)
categoryRouter.Methods("GET").Path("/all").HandlerFunc(category.GetTags)
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// @name session_id

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10000000*time.Second)
defer cancel()

log := logger.NewLogger(ctx)
Expand Down
Loading

0 comments on commit e6371d7

Please sign in to comment.