Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Consumable: blacklisting products using user's settings #19

Merged
merged 5 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions misc/consumable/consumable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package consumable

import (
"github.com/eiko-team/eiko/misc/structures"
)

// IsBoycotted check if the consumable is boycotted by the user
func IsBoycotted(c structures.Consumable, user structures.User) bool {
for _, boycotted := range user.SBoycott {
if boycotted == c.Company {
return true
}
}
return false
}

// IsBio return true is the consumable is Bio
func IsBio(structures.Consumable) bool {
// TODO: Create a function `IsBio` to filter consumables
return true
}

// IsVegan return true is the consumable is Vegan
func IsVegan(structures.Consumable) bool {
// TODO: Create a function `IsVegan` to filter consumables
return true
}

// IsHalal return true is the consumable is Halal
func IsHalal(structures.Consumable) bool {
// TODO: Create a function `IsHalal` to filter consumables
return true
}

// IsCasher return true is the consumable is Casher
func IsCasher(structures.Consumable) bool {
// TODO: Create a function `IsCasher` to filter consumables
return true
}

// ContainSodium return true is the consumable contain salt
func ContainSodium(c structures.Consumable) bool {
return c.Sodium >= 0
}

// ContainEgg return true is the consumable contain eggs
func ContainEgg(structures.Consumable) bool {
// TODO: Create a function `ContainEgg` to filter consumables
return true
}

// ContainPenut return true is the consumable contain penut
func ContainPenut(structures.Consumable) bool {
// TODO: Create a function `ContainPenut` to filter consumables
return true
}

// ContainCrustace return true is the consumable contain crustace
func ContainCrustace(structures.Consumable) bool {
// TODO: Create a function `ContainCrustace` to filter consumables
return true
}

// IsGlutenFree return true is the consumable is guten free
func IsGlutenFree(structures.Consumable) bool {
// TODO: Create a function `IsGlutenFree` to filter consumables
return true
}

// ForDiabetic return true is the consumable is suited for diabetique
func ForDiabetic(c structures.Consumable) bool {
return c.Glucides <= 0
}
37 changes: 37 additions & 0 deletions misc/consumable/consumable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package consumable_test

import (
"testing"

"github.com/eiko-team/eiko/misc/consumable"
"github.com/eiko-team/eiko/misc/structures"
)

func TestIsBoycotted(t *testing.T) {
type args struct {
}
tests := []struct {
name string
c structures.Consumable
user structures.User
result bool
}{
{"sanity", structures.Consumable{}, structures.User{}, false},
{"true", structures.Consumable{Company: "Test"},
structures.User{SBoycott: []string{"Test"}}, true},
{"false", structures.Consumable{Company: "Test"},
structures.User{SBoycott: []string{"not Test"}}, false},
{"empty boycott array", structures.Consumable{Company: "Test"},
structures.User{}, false},
{"empty company", structures.Consumable{},
structures.User{SBoycott: []string{"Test"}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.result != consumable.IsBoycotted(tt.c, tt.user) {
t.Errorf("IsBoycotted(%+v, %+v) got %t want %t",
tt.c, tt.user, consumable.IsBoycotted(tt.c, tt.user), tt.result)
}
})
}
}
11 changes: 8 additions & 3 deletions misc/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/eiko-team/eiko/misc/math"
"github.com/eiko-team/eiko/misc/research"
"github.com/eiko-team/eiko/misc/structures"
"github.com/eiko-team/eiko/misc/user"

"cloud.google.com/go/datastore"
// https://blog.nobugware.com/post/2015/leveldb_geohash_golang/
Expand Down Expand Up @@ -214,14 +215,18 @@ func (d Data) GetConsumablesTmp(query structures.Query) ([]structures.Consumable
}

res := make([]structures.Consumables, len(IDs))
for i, id := range IDs {
pos := 0
for _, id := range IDs {
c, err := d.getOneConsumable(id)
if err != nil {
return nil, err
}
res[i] = structures.Consumables{Consumable: c}
if user.IsGood(d.User, c) {
res[pos] = structures.Consumables{Consumable: c}
pos++
}
}
return res, nil
return res[:pos], nil
}

// GetConsumables is used to find some consumables in the datastore
Expand Down
44 changes: 44 additions & 0 deletions misc/structures/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ type User struct {
Pass string `firestore:"Hashed_password"`
Created time.Time `firestore:"created"`
Validated bool `firestore:"valid_user"`
SBio bool `firestore:"setting_bio"`
SVegan bool `firestore:"setting_vegan"`
SHalal bool `firestore:"setting_halal"`
SCasher bool `firestore:"setting_casher"`
SSodium bool `firestore:"setting_sodium"`
SEgg bool `firestore:"setting_egg"`
SPenut bool `firestore:"setting_arachide"`
SCrustace bool `firestore:"setting_crustace"`
SGluten bool `firestore:"setting_gluten"`
SDiabetic bool `firestore:"setting_diabetique"`
SBoycott []string `firestore:"setting_boycott"`
ID int64 // The integer ID used in the firestore.
}

Expand All @@ -31,6 +42,39 @@ func MergeUser(i1, i2 User) User {
if i1.ID == 0 {
i1.ID = i2.ID
}
if !i1.SBio {
i1.SBio = i2.SBio
}
if !i1.SVegan {
i1.SVegan = i2.SVegan
}
if !i1.SHalal {
i1.SHalal = i2.SHalal
}
if !i1.SCasher {
i1.SCasher = i2.SCasher
}
if !i1.SSodium {
i1.SSodium = i2.SSodium
}
if !i1.SEgg {
i1.SEgg = i2.SEgg
}
if !i1.SPenut {
i1.SPenut = i2.SPenut
}
if !i1.SCrustace {
i1.SCrustace = i2.SCrustace
}
if !i1.SGluten {
i1.SGluten = i2.SGluten
}
if !i1.SDiabetic {
i1.SDiabetic = i2.SDiabetic
}
if len(i1.SBoycott) != len(i2.SBoycott) {
// TODO: Implement an array merging system
}
return i1
}

Expand Down
21 changes: 21 additions & 0 deletions misc/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package user

import (
"github.com/eiko-team/eiko/misc/consumable"
"github.com/eiko-team/eiko/misc/structures"
)

// IsGood checks if the consumable is sutable for the user
func IsGood(user structures.User, c structures.Consumable) bool {
return !consumable.IsBoycotted(c, user) ||
(user.SBio && !consumable.IsBio(c)) ||
(user.SVegan && !consumable.IsVegan(c)) ||
(user.SHalal && !consumable.IsHalal(c)) ||
(user.SCasher && !consumable.IsCasher(c)) ||
(user.SSodium && !consumable.ContainSodium(c)) ||
(user.SEgg && !consumable.ContainEgg(c)) ||
(user.SPenut && !consumable.ContainPenut(c)) ||
(user.SCrustace && !consumable.ContainCrustace(c)) ||
(user.SGluten && !consumable.IsGlutenFree(c)) ||
(user.SDiabetic && !consumable.ForDiabetic(c))
}
4 changes: 2 additions & 2 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ security:
- read
- write
servers:
- url: 'https://virtserver.swaggerhub.com/tomMoulard/Eiko/1.0.0'
- url: 'https://eiko-app.com/api'
components:
responses:
Error: # $ref: '#/components/responses/Error'
Expand Down Expand Up @@ -416,7 +416,7 @@ paths:
required: true
schema:
type: string
/store/score:
/store/score:
post:
summary: Add A score to a store
description: >
Expand Down