Product Datenbank Schnittstelle angefangen
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
type Product struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
ProductIdentifier string `json:"product_identifier"`
|
||||
Properties []Property `json:"properties"`
|
||||
Products []Product `json:"products"`
|
||||
ProductStructureNodes []ProductStructureNode `json:"productStructureNodes"`
|
||||
IsEqualPropertiesEqualValues bool `json:"isEqualPropertiesEqualValues"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
type ProductStructureNode struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
IsOneOfStructure bool `json:"isOneOfStructure"`
|
||||
Products []Product `json:"products"`
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
type Productgroup struct {
|
||||
ID int `json:"id"`
|
||||
Label string `json:"label"`
|
||||
ParentProductGroupID int `json:"parentProductGroupID"`
|
||||
IsVisible bool `json:"isVisible"`
|
||||
Path string `json:"path"`
|
||||
ChildProductGroups []Productgroup `json:"childProductGroups"`
|
||||
PropertyTypes []PropertyType `json:"propertyTypes"`
|
||||
Products []Product `json:"products"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
type Property struct {
|
||||
ID int `json:"id"`
|
||||
Value string `json:"value"`
|
||||
PropertyTypeId int `json:"propertyTypeID"`
|
||||
IsTerminal bool `json:"isTerminal"`
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
"scpq-editor-server/models"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ProductgroupRepository struct {
|
||||
dbHandler *sql.DB
|
||||
m map[int]*models.Productgroup
|
||||
}
|
||||
|
||||
func NewProductgroupRepository(dbHandler *sql.DB) *ProductgroupRepository {
|
||||
return &ProductgroupRepository{
|
||||
dbHandler: dbHandler,
|
||||
m: make(map[int]*models.Productgroup),
|
||||
}
|
||||
}
|
||||
|
||||
func (rr ProductgroupRepository) CreateProductgroup(Productgroup *models.Productgroup) (*models.Productgroup, *models.ResponseError) {
|
||||
query := `INSERT INTO product_groups(label, description) VALUES($1,$2) RETURNING id`
|
||||
rows, err := rr.dbHandler.Query(query, Productgroup.Label, Productgroup.Description)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
PropertygroupId, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Propertygroup id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
pg := &models.Productgroup{
|
||||
ID: ProductgroupId,
|
||||
Label: Productgroup.Label,
|
||||
Description: Productgroup.Description,
|
||||
}
|
||||
|
||||
rr.m[pg.ID] = pg
|
||||
return pg, nil
|
||||
}
|
||||
|
||||
func (rr ProductgroupRepository) UpdateProductgroup(Productgroup *models.Productgroup) *models.ResponseError {
|
||||
query := `UPDATE product_groups SET label=$1, description=$2 WHERE id=$3`
|
||||
res, err := rr.dbHandler.Exec(query, Productgroup.Label, Productgroup.Description, Productgroup.ID)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
rowsAffeced, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if rowsAffeced == 0 {
|
||||
return &models.ResponseError{
|
||||
Message: "No Productgroup found with the given id",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
rr.m[Productgroup.ID] = Productgroup
|
||||
return nil
|
||||
}
|
||||
|
||||
func findIndex(slice []*models.Productgroup, target models.Productgroup) int {
|
||||
for i, v := range slice {
|
||||
if v.ID == target.ID {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func RemoveAt[T any](slice []T, index int) []T {
|
||||
return append(slice[:index], slice[index+1:]...)
|
||||
}
|
||||
|
||||
func (rr ProductgroupRepository) DeleteProductgroup(ProductgroupId int) *models.ResponseError {
|
||||
query := `DELETE FROM product_groups WHERE id=$1`
|
||||
res, err := rr.dbHandler.Exec(query, ProductgroupId)
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
rowsAffeced, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if rowsAffeced == 0 {
|
||||
return &models.ResponseError{
|
||||
Message: "No Productgroup found with the given id",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
rr.m[ProductgroupId] = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr ProductgroupRepository) GetProductgroup(ProductgroupId int) (*models.Productgroup, *models.ResponseError) {
|
||||
|
||||
if pg, exists := rr.m[ProductgroupId]; exists {
|
||||
return pg, nil
|
||||
}
|
||||
|
||||
query := `SELECT id, label, description FROM product_groups WHERE id=$1`
|
||||
rows, err := rr.dbHandler.Query(query, ProductgroupId)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr, label, description string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr, &label, &description)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "product group id not found",
|
||||
Status: http.StatusNotFound,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Productgroup id not int",
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
pg := &models.Productgroup{
|
||||
ID: id,
|
||||
Label: label,
|
||||
Description: description,
|
||||
}
|
||||
|
||||
rr.m[pg.ID] = pg
|
||||
return pg, nil
|
||||
}
|
||||
|
||||
func (rr ProductgroupRepository) GetAllProductgroups() ([]*models.Productgroup, *models.ResponseError) {
|
||||
var Productgroups []*models.Productgroup
|
||||
query := `SELECT id FROM product_groups`
|
||||
rows, err := rr.dbHandler.Query(query)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
var idstr string
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&idstr)
|
||||
log.Println("Productgroup id", idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: err.Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
id, err := strconv.Atoi(idstr)
|
||||
if err != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Productgroup id not int " + idstr,
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
pg, err2 := rr.GetProductgroup(id)
|
||||
if err2 != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: "Productgroup not found with the given id " + idstr,
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
if pg != nil {
|
||||
rr.m[pg.ID] = pg
|
||||
Productgroups = append(Productgroups, pg)
|
||||
}
|
||||
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
return nil, &models.ResponseError{
|
||||
Message: rows.Err().Error(),
|
||||
Status: http.StatusInternalServerError,
|
||||
}
|
||||
}
|
||||
return Productgroups, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"scpq-editor-server/repositories"
|
||||
)
|
||||
|
||||
type ProductgroupService struct {
|
||||
ProductgroupRepository *repositories.ProductgroupRepository
|
||||
}
|
||||
|
||||
func NewProductgroupService(ProductgroupRepository *repositories.ProductgroupRepository) *ProductgroupService {
|
||||
return &ProductgroupService{
|
||||
ProductgroupRepository: ProductgroupRepository,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user