0xc3 1 месяц назад
Родитель
Сommit
6c8fb34092

+ 5 - 5
src/cmd/immigration/clients/main.go

@@ -3,8 +3,8 @@ package main
 import (
 	"fmt"
 	"os"
-	clientApi "saura/src/server/client"
-	"saura/src/server/db/repo"
+	server_api "saura/src/server/api"
+	repoClient "saura/src/server/db/repo/client"
 
 	"github.com/xuri/excelize/v2"
 
@@ -13,8 +13,8 @@ import (
 
 var FILE_PATH string
 
-func ReadClientsFromFile(file_path string) ([]repo.ClientInfo, error) {
-	clients := []repo.ClientInfo{}
+func ReadClientsFromFile(file_path string) ([]repoClient.ClientInfo, error) {
+	clients := []repoClient.ClientInfo{}
 
 	file, err := excelize.OpenFile(file_path)
 	if err != nil {
@@ -52,7 +52,7 @@ func main() {
 
 		for _, client := range clients {
 			// fmt.Println(client)
-			err = clientApi.PostData[repo.ClientInfo](url, client)
+			err = server_api.PostData[repoClient.ClientInfo](url, client)
 			if err != nil {
 				fmt.Println(err)
 			}

+ 1 - 1
src/server/client/client_api.go → src/server/api/api.go

@@ -1,4 +1,4 @@
-package clientApi
+package server_api
 
 import (
 	"bytes"

+ 11 - 66
src/server/db/db.go

@@ -5,15 +5,17 @@ import (
 	"fmt"
 	"log"
 	"os"
-	"saura/src/server/db/repo"
+	repoClient "saura/src/server/db/repo/client"
+	repoUser "saura/src/server/db/repo/user"
 	"sync"
 
 	_ "github.com/mattn/go-sqlite3"
 )
 
 type DB struct {
-	db          *sql.DB
-	clientsRepo *repo.ClientRepo
+	db         *sql.DB
+	clientRepo *repoClient.ClientRepo
+	userRepo   *repoUser.UserRepo
 }
 
 var (
@@ -39,7 +41,8 @@ func (ctx *DB) Init() error {
 		return fmt.Errorf("failed to connect to database: %w", err)
 	}
 
-	ctx.clientsRepo = repo.NewClientRepo(ctx.db, dbURL)
+	ctx.clientRepo = repoClient.NewClientRepo(ctx.db, dbURL)
+	ctx.userRepo = repoUser.NewUserRepo(ctx.db, dbURL)
 
 	log.Println("Database initialized successfully")
 	return nil
@@ -52,68 +55,10 @@ func (ctx *DB) Close() error {
 	return nil
 }
 
-func (ctx *DB) GetClientRepo() *repo.ClientRepo {
-	return ctx.clientsRepo
+func (ctx *DB) GetClientRepo() *repoClient.ClientRepo {
+	return ctx.clientRepo
 }
 
-func (ctx *DB) FetchMarks() ([]string, error) {
-	query := `
-      SELECT id, name
-      FROM marks
-  `
-	rows, err := ctx.db.Query(query)
-	if err != nil {
-		return nil, fmt.Errorf("failed to execute query: %w", err)
-	}
-	defer rows.Close()
-
-	var marks []string
-
-	for rows.Next() {
-		var id string
-		var name string
-
-		if err := rows.Scan(&id, &name); err != nil {
-			return nil, fmt.Errorf("failed to scan row: %w", err)
-		}
-
-		marks = append(marks, name)
-	}
-
-	if err := rows.Err(); err != nil {
-		return nil, fmt.Errorf("error during iteration: %w", err)
-	}
-
-	return marks, nil
-}
-
-func (ctx *DB) FetchAdChannels() ([]string, error) {
-	query := `
-      SELECT id, name
-      FROM ad_channels
-  `
-	rows, err := ctx.db.Query(query)
-	if err != nil {
-		return nil, fmt.Errorf("failed to execute query: %w", err)
-	}
-	defer rows.Close()
-
-	var ad_channels []string
-
-	for rows.Next() {
-		var id string
-		var name string
-
-		if err := rows.Scan(&id, &name); err != nil {
-			return nil, fmt.Errorf("failed to scan row: %w", err)
-		}
-
-		ad_channels = append(ad_channels, name)
-	}
-
-	if err := rows.Err(); err != nil {
-		return nil, fmt.Errorf("error during iteration: %w", err)
-	}
-
-	return ad_channels, nil
+func (ctx *DB) GetUserRepo() *repoUser.UserRepo {
+	return ctx.userRepo
 }

+ 1 - 4
src/server/db/repo/client.go → src/server/db/repo/client/client.go

@@ -1,8 +1,7 @@
-package repo
+package client
 
 import (
 	"database/sql"
-	"math/rand"
 	"time"
 
 	"github.com/google/uuid"
@@ -75,8 +74,6 @@ func (r *ClientRepo) FetchClients(limit int, offset int) ([]ClientInfo, error) {
 }
 
 func (r *ClientRepo) FetchExampleClients(limit int, offset int) []ClientInfo {
-	rand.Seed(42)
-
 	clients := []ClientInfo{
 		{
 			ID:              uuid.New().String(),

+ 77 - 0
src/server/db/repo/user/user.go

@@ -0,0 +1,77 @@
+package user
+
+import (
+	"database/sql"
+	"time"
+
+	"github.com/google/uuid"
+)
+
+type UserRepo struct {
+	db    *sql.DB
+	dbUrl string
+}
+
+type UserType string
+
+const (
+	UserRoleReceiver UserType = "Приемщик"
+	UserRoleDirector UserType = "Директор"
+	UserRoleEngineer UserType = "Инженер"
+)
+
+func (ut UserType) IsValid() bool {
+	switch ut {
+	case
+		UserRoleReceiver,
+		UserRoleDirector,
+		UserRoleEngineer:
+		return true
+	default:
+		return false
+	}
+}
+
+type UserInfo struct {
+	ID       string   `db:"id" json:"id"`
+	Login    string   `db:"login" json:"login"`
+	Password string   `db:"password" json:"password"`
+	Phone    string   `db:"phone" json:"phone"`
+	Email    string   `db:"email" json:"email"`
+	FullName string   `db:"full_name" json:"full_name"`
+	Role     UserType `db:"role" json:"role"`
+	RegDate  string   `db:"reg_date" json:"reg_date"`
+}
+
+func NewUserRepo(db *sql.DB, dbUrl string) *UserRepo {
+	return &UserRepo{db: db, dbUrl: dbUrl}
+}
+
+func (r *UserRepo) FetchUsers(limit int, offset int) ([]UserInfo, error) {
+	var users = []UserInfo{}
+	return users[offset:limit], nil
+}
+
+func (r *UserRepo) FetchExampleUsers(limit int, offset int) []UserInfo {
+	users := []UserInfo{
+		{
+			ID:       uuid.New().String(),
+			Login:    "ivan",
+			Password: "",
+			Phone:    "+79001234567",
+			Email:    "ivanov@example.com",
+			FullName: "Иванов Иван Иванович",
+			Role:     UserRoleDirector,
+			RegDate:  time.Now().Format("02.01.2006 15:04"),
+		},
+	}
+
+	if offset >= len(users) {
+		return []UserInfo{}
+	}
+	if limit+offset > len(users) {
+		limit = len(users) - offset
+	}
+
+	return users[offset : offset+limit]
+}

+ 8 - 0
src/server/server.go

@@ -79,5 +79,13 @@ func RegisterHandlers(db *db.DB) http.Handler {
 		return c.JSON(http.StatusOK, clients)
 	})
 
+	apiGroup.GET("/v1/example/users", func(c echo.Context) error {
+		limit, _ := strconv.Atoi(c.QueryParam("limit"))
+		offset, _ := strconv.Atoi(c.QueryParam("offset"))
+
+		users := db.GetUserRepo().FetchExampleUsers(limit, offset)
+		return c.JSON(http.StatusOK, users)
+	})
+
 	return e
 }