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()
}
func IsNameInFutureProducts(name string) bool {
func IsIdInFutureProducts(id string) bool {
pipelineProducts := GetPipelineProducts()
for _, pipelineProduct := range pipelineProducts.FutureProducts {
if pipelineProduct.Name == name {
if pipelineProduct.Id == id {
return true
}
}
@ -94,17 +94,16 @@ func IsNameInFutureProducts(name string) bool {
return false
}
func VoteFutureProduct(name string, up bool) {
func VoteFutureProduct(id string, up bool) {
pipelineProducts := GetPipelineProducts()
for index, futureProduct := range pipelineProducts.FutureProducts {
if futureProduct.Name == name {
if futureProduct.Id == id {
pp.Lock()
if up {
pipelineProductsCache.FutureProducts[index].Votes++
} else {
pipelineProductsCache.FutureProducts[index].Votes--
}

View File

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

View File

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