Skip to content

Commit

Permalink
Merge pull request #22 from aau-network-security/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mrtrkmn authored Aug 19, 2021
2 parents fd0d2f2 + b0f381a commit 150f673
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 65 deletions.
7 changes: 4 additions & 3 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import "go.mongodb.org/mongo-driver/bson/primitive"
type Tag string

type Category struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Tag Tag `bson:"tag,omitempty"`
Name string `bson:"name,omitempty"`
ID primitive.ObjectID `bson:"_id,omitempty"`
Tag Tag `bson:"tag,omitempty"`
Name string `bson:"name,omitempty"`
CatDescription string `bson:"cd,omitempty"`
}

//todo manage the status somehow
Expand Down
125 changes: 72 additions & 53 deletions proto/exercise.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/exercise.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ message GetCategoriesResponse{
message Category {
string tag = 1;
string name = 2;
string catDesc = 3;
}
repeated Category categories = 1;
}
Expand All @@ -86,6 +87,7 @@ message AddExerciseRequest{
message AddCategoryRequest{
string tag = 1;
string name = 2;
string catDescription = 3;
}

message ResponseStatus{}
3 changes: 2 additions & 1 deletion server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s *Server) GetCategories(ctx context.Context, empty *pb.Empty) (*pb.GetCat
categs = append(categs, &pb.GetCategoriesResponse_Category{
Tag: string(c.Tag),
Name: c.Name,
CatDesc: c.CatDescription,
})
}

Expand All @@ -114,7 +115,7 @@ func (s *Server) GetCategories(ctx context.Context, empty *pb.Empty) (*pb.GetCat
func (s *Server) AddCategory(ctx context.Context, request *pb.AddCategoryRequest) (*pb.ResponseStatus, error) {

log.Printf("ADD category [%s]", request.Tag)
if err := s.store.AddCategory(request.Tag, request.Name); err != nil {
if err := s.store.AddCategory(request.Tag, request.Name, request.CatDesc); err != nil {
return nil, err
}
return &pb.ResponseStatus{}, nil
Expand Down
6 changes: 4 additions & 2 deletions server/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,19 @@ func TestServer_AddCategory(t *testing.T) {
tt := []struct {
name string
categ string
description string
err bool
}{
{name: "Normal category", categ: "randomcategory"},
{name: "Already existing category", categ: "randomcategory", err: true},
{name: "Normal category", categ: "randomcategory", description: "Some description"},
{name: "Already existing category", categ: "randomcategory", description: "Some description", err: true},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
_, err := c.AddCategory(context.Background(), &pb.AddCategoryRequest{
Tag: tc.categ,
Name: tc.categ,
CatDesc: tc.description,
})
if err != nil {
if tc.err {
Expand Down
5 changes: 3 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Store interface {
GetExerciseByCategory(string) ([]model.Exercise, error)
GetCategories() []model.Category
GetCategoryName(primitive.ObjectID) string
AddCategory(string, string) error
AddCategory(string, string, string) error
AddExercise(string) error
UpdateCache() error
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func (s *store) GetCategoryName(obj primitive.ObjectID) string {
return ""
}

func (s *store) AddCategory(tag string, name string) error {
func (s *store) AddCategory(tag string, name string, catDesc string) error {
s.m.Lock()
defer s.m.Unlock()

Expand All @@ -151,6 +151,7 @@ func (s *store) AddCategory(tag string, name string) error {
category := model.Category{
Tag: categoryTag, // FR,
Name: name, // Forensics,
CatDescription: catDesc,
}
_, err := collection.InsertOne(ctx, category)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,17 @@ func TestStore_AddCategory(t *testing.T) {
tt := []struct {
name string
categ string
description string
err bool
}{
{name: "Normal category", categ: "new"},
{name: "Already existing category", categ: NCategories[0], err: true},
{name: "Normal category 2", categ: "new", err: true},
{name: "Normal category", categ: "new", description: "Some description"},
{name: "Already existing category", categ: NCategories[0], description: "Some description", err: true},
{name: "Normal category 2", categ: "new", description: "Some description", err: true},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if err := s.AddCategory(tc.categ, tc.categ); err != nil {
if err := s.AddCategory(tc.categ, tc.categ, tc.description); err != nil {
if tc.err {
return
}
Expand Down

0 comments on commit 150f673

Please sign in to comment.