Browse Source

add category model
add DOCKER

portowyi 1 year ago
parent
commit
cacc148197
5 changed files with 143 additions and 0 deletions
  1. 12 0
      Dockerfile
  2. 24 0
      controller/category.go
  3. 17 0
      cron-job/cronJobs.go
  4. 59 0
      docker-compose.yml
  5. 31 0
      model/category.go

+ 12 - 0
Dockerfile

@@ -0,0 +1,12 @@
+FROM golang:1.23
+
+WORKDIR /usr/src/app
+
+# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
+COPY go.mod go.sum ./
+
+RUN go mod download && go mod verify
+
+COPY . .
+
+CMD ["go", "run", "main.go"]

+ 24 - 0
controller/category.go

@@ -0,0 +1,24 @@
+package controller
+
+import (
+	"kng_feed_api/model"
+	"net/http"
+
+	"github.com/gin-gonic/gin"
+)
+
+func CreateUpdateCategories(context *gin.Context) {
+	var categories model.Categories
+	err := context.ShouldBindJSON(&categories)
+	if err != nil {
+		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error})
+	}
+
+	for _, cat := range categories.Categories {
+		_, err := cat.Save()
+		if err != nil {
+			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error, "desc": "Error whyle saving data"})
+			return
+		}
+	}
+}

+ 17 - 0
cron-job/cronJobs.go

@@ -48,6 +48,12 @@ func CreateXMLFeed() {
 	}
 
 	fmt.Println("CRON JOB WORK - FROM EXTERNAL FILE ", time.Now())
+
+	categoryRows, err := model.GetAllCategories()
+	if err != nil {
+		return
+	}
+
 	rows, err := model.GetAllProducts()
 	if err != nil {
 		return
@@ -62,6 +68,17 @@ func CreateXMLFeed() {
 	shop.Name = "ООО \"Княгиня\""
 	shop.URL = "https://b2bn.kngnn.ru"
 
+	for _, value := range categoryRows {
+		var category = Category{
+			Text:     value.Name,
+			ParentId: value.ParentId,
+			Id:       value.Id,
+			URL:      value.Id,
+		}
+
+		shop.Category = append(shop.Category, category)
+	}
+
 	for _, value := range rows {
 		var offer = Offer{
 			Name:       value.Name,

+ 59 - 0
docker-compose.yml

@@ -0,0 +1,59 @@
+version: "3.9"
+services:
+  postgres:
+    image: postgres:15.3-alpine
+    container_name: pg_api
+    environment:
+      POSTGRES_USER: ${DB_USER}
+      POSTGRES_PASSWORD: ${DB_PASSWORD}
+      POSTGRES_DB: ${DB_DATABASE_NAME}
+    ports:
+      - 5432:5432
+    expose:
+      - "6500"
+    volumes:
+      - postgres:/var/lib/postgresql/data
+    healthcheck:
+      test: ["CMD", "pg_isready", "-h", "postgres", "-U", "${DB_USER}"]
+      interval: 5s
+      timeout: 5s
+      retries: 5
+    networks:
+      - api_network
+
+  pgadmin:
+    image: dpage/pgadmin4
+    container_name: pgadmin_tefo
+    environment:
+      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
+      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
+      PGADMIN_LISTEN_PORT: ${PGADMIN_LISTEN_PORT}
+    ports:
+      - 5050:80
+    volumes:
+      - pgadmin-data:/var/lib/pgadmin
+    restart: "no"
+    depends_on:
+      - postgres
+    networks:
+      - api_network
+
+  app:
+    build:
+      dockerfile: Dockerfile
+      context: .
+    volumes:
+      - .:/usr/src/app
+    environment:
+      - 3005:3005
+    ports:
+      - "3005:3005"
+    networks:
+      - api_network
+
+volumes:
+  postgres:
+  pgadmin-data:
+
+networks:
+  api_network:

+ 31 - 0
model/category.go

@@ -0,0 +1,31 @@
+package model
+
+import "kng_feed_api/database"
+
+type Category struct {
+	Id       string `gorm:"size:3;not null;unique;index;primary key" json:"id"`
+	Name     string `gorm:"size:100" json:"name"`
+	ParentId string `gorm:"size:3" json:"parent_id"`
+	Url      string `gorm:"size:3"`
+}
+
+type Categories struct {
+	Categories []Category `binding:"dive" json:"categories"`
+}
+
+func (category *Category) Save() (*Category, error) {
+	err := database.Database.Save(&category).Error
+	if err != nil {
+		return &Category{}, err
+	}
+	return category, nil
+}
+
+func GetAllCategories() ([]Category, error) {
+	var categories []Category
+	result := database.Database.Find(&categories)
+	if result.Error != nil {
+		return categories, result.Error
+	}
+	return categories, nil
+}