chchanged unique name to id

main
alex 2024-06-02 10:25:32 +02:00
parent cbf312ed48
commit ddbe297947
4 changed files with 33 additions and 25 deletions

BIN
main

Binary file not shown.

View File

@ -82,11 +82,11 @@ func SetProductStates(productStates []structs.ProductState) {
ps.Unlock() ps.Unlock()
} }
func IsNameInFutureProducts(name string) bool { func IsIdInFutureProducts(id string) bool {
pipelineProducts := GetPipelineProducts() pipelineProducts := GetPipelineProducts()
for _, pipelineProduct := range pipelineProducts.FutureProducts { for _, pipelineProduct := range pipelineProducts.FutureProducts {
if pipelineProduct.Name == name { if pipelineProduct.Id == id {
return true return true
} }
} }
@ -94,17 +94,16 @@ func IsNameInFutureProducts(name string) bool {
return false return false
} }
func VoteFutureProduct(name string, up bool) { func VoteFutureProduct(id string, up bool) {
pipelineProducts := GetPipelineProducts() pipelineProducts := GetPipelineProducts()
for index, futureProduct := range pipelineProducts.FutureProducts { for index, futureProduct := range pipelineProducts.FutureProducts {
if futureProduct.Name == name { if futureProduct.Id == id {
pp.Lock() pp.Lock()
if up { if up {
pipelineProductsCache.FutureProducts[index].Votes++ pipelineProductsCache.FutureProducts[index].Votes++
} else { } else {
pipelineProductsCache.FutureProducts[index].Votes-- pipelineProductsCache.FutureProducts[index].Votes--
} }

View File

@ -2,11 +2,13 @@ package structs
// database model // database model
type PipelineProduct struct { type PipelineProduct struct {
Name string `gorm:"primaryKey"` // name is used as ID Id string
Name string
Votes int Votes int
} }
type NewProduct struct { type NewProduct struct {
Id string
Name string Name string
Url string Url string
Variant string Variant string
@ -14,12 +16,14 @@ type NewProduct struct {
} }
type InWorkProduct struct { type InWorkProduct struct {
Id string
Name string Name string
Variant string Variant string
Characteristics string Characteristics string
} }
type FutureProduct struct { type FutureProduct struct {
Id string
Name string Name string
Votes int Votes int
Variant string Variant string
@ -43,5 +47,5 @@ type VoteProductQuery struct {
} }
type VoteProductRequest struct { type VoteProductRequest struct {
Name string Id string
} }

View File

@ -114,31 +114,33 @@ func fetchProducts(srv *sheets.Service) error {
var futureProducts []structs.FutureProduct var futureProducts []structs.FutureProduct
for _, row := range resp.Values[1:] { // skip first google worksheet row for _, row := range resp.Values[1:] { // skip first google worksheet row
// skip to next if status or name is empty // skip to next if id, status or name is empty
if row[0] == "" || row[1] == "" { if row[0] == "" || row[1] == "" || row[2] == "" {
continue continue
} }
status := fmt.Sprintf("%v", row[0]) productId := fmt.Sprintf("%v", row[0])
name := fmt.Sprintf("%v", row[1]) status := fmt.Sprintf("%v", row[1])
name := fmt.Sprintf("%v", row[2])
var productVariant string var productVariant string
if row[2] != "" { if row[3] != "" {
productVariant = fmt.Sprintf("%v", row[2]) productVariant = fmt.Sprintf("%v", row[3])
} }
var productCharacteristics string var productCharacteristics string
if row[3] != "" { if row[4] != "" {
productCharacteristics = fmt.Sprintf("%v", row[3]) productCharacteristics = fmt.Sprintf("%v", row[4])
} }
state := getStateByStatus(status) state := getStateByStatus(status)
// add product to database if not exist // add product to database if not exist
if !isPipelineProductInDatabase(databasePipelineProducts, name) { if !isPipelineProductInDatabase(databasePipelineProducts, productId) {
database.DB.Create(&structs.PipelineProduct{ database.DB.Create(&structs.PipelineProduct{
Id: productId,
Name: name, Name: name,
Votes: 0, Votes: 0,
}) })
@ -148,6 +150,7 @@ func fetchProducts(srv *sheets.Service) error {
if state == 1 { if state == 1 {
futureProducts = append(futureProducts, structs.FutureProduct{ futureProducts = append(futureProducts, structs.FutureProduct{
Id: productId,
Name: name, Name: name,
Votes: getVotes(databasePipelineProducts, name), // votes from database Votes: getVotes(databasePipelineProducts, name), // votes from database
Variant: productVariant, Variant: productVariant,
@ -155,6 +158,7 @@ func fetchProducts(srv *sheets.Service) error {
}) })
} else if state == 2 { } else if state == 2 {
inWorkProducts = append(inWorkProducts, structs.InWorkProduct{ inWorkProducts = append(inWorkProducts, structs.InWorkProduct{
Id: productId,
Name: name, Name: name,
Variant: productVariant, Variant: productVariant,
Characteristics: productCharacteristics, Characteristics: productCharacteristics,
@ -162,11 +166,12 @@ func fetchProducts(srv *sheets.Service) error {
} else if state == 3 { } else if state == 3 {
var url string var url string
if row[7] != "" { if row[8] != "" {
url = fmt.Sprintf("%v", row[7]) url = fmt.Sprintf("%v", row[8])
} }
newProducts = append(newProducts, structs.NewProduct{ newProducts = append(newProducts, structs.NewProduct{
Id: productId,
Name: name, Name: name,
Url: url, Url: url,
Variant: productVariant, Variant: productVariant,
@ -236,9 +241,9 @@ func fetchProductStates(srv *sheets.Service) error {
return nil return nil
} }
func isPipelineProductInDatabase(pipelineProducts []structs.PipelineProduct, name string) bool { func isPipelineProductInDatabase(pipelineProducts []structs.PipelineProduct, id string) bool {
for _, pipelineProduct := range pipelineProducts { for _, pipelineProduct := range pipelineProducts {
if pipelineProduct.Name == name { if pipelineProduct.Id == id {
return true return true
} }
} }
@ -264,8 +269,8 @@ func VoteProduct(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusBadRequest) return c.SendStatus(fiber.StatusBadRequest)
} }
if !cache.IsNameInFutureProducts(body.Name) { if !cache.IsIdInFutureProducts(body.Id) {
logger.AddSystemLog(rslogger.LogTypeWarning, "VoteProduct invalid product name provided, name: %s", body.Name) logger.AddSystemLog(rslogger.LogTypeWarning, "VoteProduct invalid product id provided, id: %s", body.Id)
return c.SendStatus(fiber.StatusBadRequest) return c.SendStatus(fiber.StatusBadRequest)
} }
@ -275,12 +280,12 @@ func VoteProduct(c *fiber.Ctx) error {
operator = "-" operator = "-"
} }
database.DB.Model(&structs.PipelineProduct{}).Where("name = ?", body.Name).Update("votes", gorm.Expr(fmt.Sprintf("votes %s ?", operator), 1)) database.DB.Model(&structs.PipelineProduct{}).Where("id = ?", body.Id).Update("votes", gorm.Expr(fmt.Sprintf("votes %s ?", operator), 1))
cache.VoteFutureProduct(body.Name, query.T == "u") cache.VoteFutureProduct(body.Id, query.T == "u")
cache.SortFutureProducts() cache.SortFutureProducts()
logger.AddSystemLog(rslogger.LogTypeInfo, "Vote name: %v up vote: %v", body.Name, query.T) logger.AddSystemLog(rslogger.LogTypeInfo, "Vote name: %v up vote: %v", body.Id, query.T)
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
} }