From fb9d9bbd56215a8de464fecd27b30997d141539f Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Thu, 30 Dec 2021 10:29:59 +0530 Subject: [PATCH 01/12] moved db models to db package --- internal/datastore/mongo_store.go | 20 ++++++++++---------- internal/datastore/store.go | 18 +++++++++--------- internal/handler/queue.go | 6 +++--- internal/models/common.go | 3 --- internal/models/db/common.go | 3 +++ internal/models/{ => db}/queue.go | 2 +- internal/models/{ => db}/token.go | 2 +- 7 files changed, 27 insertions(+), 27 deletions(-) delete mode 100644 internal/models/common.go create mode 100644 internal/models/db/common.go rename internal/models/{ => db}/queue.go (98%) rename internal/models/{ => db}/token.go (97%) diff --git a/internal/datastore/mongo_store.go b/internal/datastore/mongo_store.go index 6d3ee17..57951d6 100644 --- a/internal/datastore/mongo_store.go +++ b/internal/datastore/mongo_store.go @@ -5,7 +5,7 @@ import ( "log" "os" - "github.com/SimplQ/simplQ-golang/internal/models" + "github.com/SimplQ/simplQ-golang/internal/models/db" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" @@ -46,7 +46,7 @@ func NewMongoDB() *MongoDB { return &new_mongodb } -func (mongodb MongoDB) CreateQueue(queue models.Queue) models.QueueId { +func (mongodb MongoDB) CreateQueue(queue db.Queue) db.QueueId { // Set id to empty so its generated by mongoDB queue.Id = "" @@ -58,33 +58,33 @@ func (mongodb MongoDB) CreateQueue(queue models.Queue) models.QueueId { stringId := result.InsertedID.(primitive.ObjectID).Hex() - return models.QueueId(stringId) + return db.QueueId(stringId) } -func (mongodb MongoDB) ReadQueue(models.QueueId) models.Queue { +func (mongodb MongoDB) ReadQueue(db.QueueId) db.Queue { panic("Not implemented") } -func (mongodb MongoDB) PauseQueue(models.QueueId) { +func (mongodb MongoDB) PauseQueue(db.QueueId) { panic("Not implemented") } -func (mongodb MongoDB) ResumeQueue(models.QueueId) { +func (mongodb MongoDB) ResumeQueue(db.QueueId) { panic("Not implemented") } -func (mongodb MongoDB) DeleteQueue(models.QueueId) { +func (mongodb MongoDB) DeleteQueue(db.QueueId) { panic("Not implemented") } -func (mongodb MongoDB) AddTokenToQueue(models.QueueId, models.Token) { +func (mongodb MongoDB) AddTokenToQueue(db.QueueId, db.Token) { panic("Not implemented") } -func (mongodb MongoDB) ReadToken(models.TokenId) { +func (mongodb MongoDB) ReadToken(db.TokenId) { panic("Not implemented") } -func (mongodb MongoDB) RemoveToken(models.TokenId) { +func (mongodb MongoDB) RemoveToken(db.TokenId) { panic("Not implemented") } diff --git a/internal/datastore/store.go b/internal/datastore/store.go index 4cf694b..e2b02f3 100644 --- a/internal/datastore/store.go +++ b/internal/datastore/store.go @@ -1,31 +1,31 @@ package datastore -import "github.com/SimplQ/simplQ-golang/internal/models" +import "github.com/SimplQ/simplQ-golang/internal/models/db" type QueueStore interface { // Create a new queue and return the queue ID. - CreateQueue(models.Queue) models.QueueId + CreateQueue(db.Queue) db.QueueId // Read a queue by id. - ReadQueue(models.QueueId) models.Queue + ReadQueue(db.QueueId) db.Queue // Set the queue pause status to true - PauseQueue(models.QueueId) + PauseQueue(db.QueueId) // Set the queue pause status to false - ResumeQueue(models.QueueId) + ResumeQueue(db.QueueId) // Set the queue delete status to new value. - DeleteQueue(models.QueueId) + DeleteQueue(db.QueueId) // Add a new token to the queue. - AddTokenToQueue(models.QueueId, models.Token) + AddTokenToQueue(db.QueueId, db.Token) // Read token by id. - ReadToken(models.TokenId) + ReadToken(db.TokenId) // Delete token - RemoveToken(models.TokenId) + RemoveToken(db.TokenId) } var Store QueueStore = NewMongoDB() diff --git a/internal/handler/queue.go b/internal/handler/queue.go index fb8d05c..bdcf73a 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -8,7 +8,7 @@ import ( "time" "github.com/SimplQ/simplQ-golang/internal/datastore" - "github.com/SimplQ/simplQ-golang/internal/models" + "github.com/SimplQ/simplQ-golang/internal/models/db" ) func GetQueue(w http.ResponseWriter, r *http.Request) { @@ -18,7 +18,7 @@ func GetQueue(w http.ResponseWriter, r *http.Request) { func CreateQueue(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) - var q models.Queue + var q db.Queue err := decoder.Decode(&q) if err != nil { @@ -30,7 +30,7 @@ func CreateQueue(w http.ResponseWriter, r *http.Request) { q.CreationTime = time.Now() q.IsDeleted = false q.IsPaused = false - q.Tokens = make([]models.Token, 0) + q.Tokens = make([]db.Token, 0) log.Print("Create Queue: ") log.Println(q) diff --git a/internal/models/common.go b/internal/models/common.go deleted file mode 100644 index 1ee10e1..0000000 --- a/internal/models/common.go +++ /dev/null @@ -1,3 +0,0 @@ -package models - -type Id string \ No newline at end of file diff --git a/internal/models/db/common.go b/internal/models/db/common.go new file mode 100644 index 0000000..ca63bee --- /dev/null +++ b/internal/models/db/common.go @@ -0,0 +1,3 @@ +package db + +type Id string diff --git a/internal/models/queue.go b/internal/models/db/queue.go similarity index 98% rename from internal/models/queue.go rename to internal/models/db/queue.go index b10ef2b..d72bd08 100644 --- a/internal/models/queue.go +++ b/internal/models/db/queue.go @@ -1,4 +1,4 @@ -package models +package db import ( "time" diff --git a/internal/models/token.go b/internal/models/db/token.go similarity index 97% rename from internal/models/token.go rename to internal/models/db/token.go index 4b5f1e5..cee7cc3 100644 --- a/internal/models/token.go +++ b/internal/models/db/token.go @@ -1,4 +1,4 @@ -package models +package db import ( "time" From a7e98fa01c40b0eeb7ca04663c19b2141cbc231e Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Thu, 30 Dec 2021 10:56:12 +0530 Subject: [PATCH 02/12] added api models and queuename validation --- internal/handler/queue.go | 25 ++++++++++++++++++------- internal/models/api/common.go | 10 ++++++++++ internal/models/api/queue.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 internal/models/api/common.go create mode 100644 internal/models/api/queue.go diff --git a/internal/handler/queue.go b/internal/handler/queue.go index bdcf73a..ecc9945 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -9,6 +9,7 @@ import ( "github.com/SimplQ/simplQ-golang/internal/datastore" "github.com/SimplQ/simplQ-golang/internal/models/db" + "github.com/SimplQ/simplQ-golang/internal/models/api" ) func GetQueue(w http.ResponseWriter, r *http.Request) { @@ -18,24 +19,34 @@ func GetQueue(w http.ResponseWriter, r *http.Request) { func CreateQueue(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) - var q db.Queue + var q api.CreateQueueRequest err := decoder.Decode(&q) if err != nil { - panic(err) + http.Error(w, fmt.Sprint(err), http.StatusBadRequest) + } + + // Validation + validation_err, ok := q.Validate() + + if !ok { + http.Error(w, validation_err.Message, http.StatusBadRequest) } // Initialize values // Only consider queue name from the body of the request - q.CreationTime = time.Now() - q.IsDeleted = false - q.IsPaused = false - q.Tokens = make([]db.Token, 0) + queue := db.Queue { + QueueName: q.QueueName, + CreationTime: time.Now(), + IsDeleted: false, + IsPaused: false, + Tokens: make([]db.Token, 0), + } log.Print("Create Queue: ") log.Println(q) - insertedId := datastore.Store.CreateQueue(q) + insertedId := datastore.Store.CreateQueue(queue) log.Printf("Inserted %s", insertedId) diff --git a/internal/models/api/common.go b/internal/models/api/common.go new file mode 100644 index 0000000..8c3a0e2 --- /dev/null +++ b/internal/models/api/common.go @@ -0,0 +1,10 @@ +package api + +type ValidationError struct { + Fields []string + Message string +} + +type Validator interface { + Validate() (ValidationError, bool) +} diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go new file mode 100644 index 0000000..afb09d4 --- /dev/null +++ b/internal/models/api/queue.go @@ -0,0 +1,28 @@ +package api + +import ( + "fmt" + + "github.com/SimplQ/simplQ-golang/internal/models/db" +) + +type CreateQueueRequest struct { + QueueName string +} + +type CreateQueueResponse db.Queue + +const MIN_LENGTH = 4 +const MAX_LENGTH = 20 + +func (req CreateQueueRequest) Validate() (ValidationError, bool) { + if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { + message := fmt.Sprintf("Queue Name should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) + return ValidationError { + Fields: []string{"QueueName"}, + Message: message, + }, false + } + + return ValidationError{}, true +} From f6def182ed6e6758f11d64537c6c7db59b629072 Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Wed, 12 Jan 2022 18:48:06 +0530 Subject: [PATCH 03/12] fixed error and descriptive variable --- internal/handler/queue.go | 13 +++++++------ internal/models/api/queue.go | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/handler/queue.go b/internal/handler/queue.go index ecc9945..71cd660 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -19,24 +19,25 @@ func GetQueue(w http.ResponseWriter, r *http.Request) { func CreateQueue(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) - var q api.CreateQueueRequest - err := decoder.Decode(&q) + var queueRequest api.CreateQueueRequest + err := decoder.Decode(&queueRequest) if err != nil { http.Error(w, fmt.Sprint(err), http.StatusBadRequest) } // Validation - validation_err, ok := q.Validate() + validationErr, ok := queueRequest.Validate() if !ok { - http.Error(w, validation_err.Message, http.StatusBadRequest) + http.Error(w, validationErr.Message, http.StatusBadRequest) + return } // Initialize values // Only consider queue name from the body of the request queue := db.Queue { - QueueName: q.QueueName, + QueueName: queueRequest.QueueName, CreationTime: time.Now(), IsDeleted: false, IsPaused: false, @@ -44,7 +45,7 @@ func CreateQueue(w http.ResponseWriter, r *http.Request) { } log.Print("Create Queue: ") - log.Println(q) + log.Println(queueRequest) insertedId := datastore.Store.CreateQueue(queue) diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index afb09d4..bd96fcb 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -17,7 +17,7 @@ const MAX_LENGTH = 20 func (req CreateQueueRequest) Validate() (ValidationError, bool) { if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { - message := fmt.Sprintf("Queue Name should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) + message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) return ValidationError { Fields: []string{"QueueName"}, Message: message, From 4198f30fe37e852b9ba5eedd3fd30732cd8469de Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Wed, 12 Jan 2022 18:55:32 +0530 Subject: [PATCH 04/12] single field name for error --- internal/models/api/common.go | 2 +- internal/models/api/queue.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/models/api/common.go b/internal/models/api/common.go index 8c3a0e2..48651c0 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -1,7 +1,7 @@ package api type ValidationError struct { - Fields []string + Field string Message string } diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index bd96fcb..6aec88b 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -19,7 +19,7 @@ func (req CreateQueueRequest) Validate() (ValidationError, bool) { if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) return ValidationError { - Fields: []string{"QueueName"}, + Field: "QueueName", Message: message, }, false } From 0d02e4c221abf90febc7af319f981c30142948ed Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 18 Jan 2022 05:16:17 +0000 Subject: [PATCH 05/12] Fix code style issues with gofmt --- internal/datastore/mongo_store.go | 20 +++++----- internal/datastore/store.go | 6 +-- internal/handler/queue.go | 64 +++++++++++++++---------------- internal/models/api/common.go | 6 +-- internal/models/api/queue.go | 22 +++++------ 5 files changed, 59 insertions(+), 59 deletions(-) diff --git a/internal/datastore/mongo_store.go b/internal/datastore/mongo_store.go index b8db809..da7a5c3 100644 --- a/internal/datastore/mongo_store.go +++ b/internal/datastore/mongo_store.go @@ -47,8 +47,8 @@ func NewMongoDB() *MongoDB { } func (mongodb MongoDB) CreateQueue(queue db.Queue) db.QueueId { - // Set id to empty so its generated by mongoDB - queue.Id = "" + // Set id to empty so its generated by mongoDB + queue.Id = "" result, err := mongodb.Queue.InsertOne(context.TODO(), queue) @@ -58,33 +58,33 @@ func (mongodb MongoDB) CreateQueue(queue db.Queue) db.QueueId { stringId := result.InsertedID.(primitive.ObjectID).Hex() - return db.QueueId(stringId) + return db.QueueId(stringId) } func (mongodb MongoDB) ReadQueue(db.QueueId) db.Queue { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) PauseQueue(db.QueueId) { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) ResumeQueue(db.QueueId) { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) DeleteQueue(db.QueueId) { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) AddTokenToQueue(db.QueueId, db.Token) { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) ReadToken(db.TokenId) { - panic("Not implemented") + panic("Not implemented") } func (mongodb MongoDB) RemoveToken(db.TokenId) { - panic("Not implemented") + panic("Not implemented") } diff --git a/internal/datastore/store.go b/internal/datastore/store.go index e2b02f3..a02c661 100644 --- a/internal/datastore/store.go +++ b/internal/datastore/store.go @@ -12,10 +12,10 @@ type QueueStore interface { // Set the queue pause status to true PauseQueue(db.QueueId) - // Set the queue pause status to false + // Set the queue pause status to false ResumeQueue(db.QueueId) - - // Set the queue delete status to new value. + + // Set the queue delete status to new value. DeleteQueue(db.QueueId) // Add a new token to the queue. diff --git a/internal/handler/queue.go b/internal/handler/queue.go index d4c565e..fa391ed 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -8,8 +8,8 @@ import ( "time" "github.com/SimplQ/simplQ-golang/internal/datastore" - "github.com/SimplQ/simplQ-golang/internal/models/db" "github.com/SimplQ/simplQ-golang/internal/models/api" + "github.com/SimplQ/simplQ-golang/internal/models/db" ) func GetQueue(w http.ResponseWriter, r *http.Request) { @@ -19,37 +19,37 @@ func GetQueue(w http.ResponseWriter, r *http.Request) { func CreateQueue(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body) - var queueRequest api.CreateQueueRequest - err := decoder.Decode(&queueRequest) - - if err != nil { - http.Error(w, fmt.Sprint(err), http.StatusBadRequest) - } - - // Validation - validationErr, ok := queueRequest.Validate() - - if !ok { - http.Error(w, validationErr.Message, http.StatusBadRequest) - return - } - - // Initialize values - // Only consider queue name from the body of the request - queue := db.Queue { - QueueName: queueRequest.QueueName, - CreationTime: time.Now(), - IsDeleted: false, - IsPaused: false, - Tokens: make([]db.Token, 0), - } - - log.Print("Create Queue: ") - log.Println(queueRequest) - - insertedId := datastore.Store.CreateQueue(queue) - - log.Printf("Inserted %s", insertedId) + var queueRequest api.CreateQueueRequest + err := decoder.Decode(&queueRequest) + + if err != nil { + http.Error(w, fmt.Sprint(err), http.StatusBadRequest) + } + + // Validation + validationErr, ok := queueRequest.Validate() + + if !ok { + http.Error(w, validationErr.Message, http.StatusBadRequest) + return + } + + // Initialize values + // Only consider queue name from the body of the request + queue := db.Queue{ + QueueName: queueRequest.QueueName, + CreationTime: time.Now(), + IsDeleted: false, + IsPaused: false, + Tokens: make([]db.Token, 0), + } + + log.Print("Create Queue: ") + log.Println(queueRequest) + + insertedId := datastore.Store.CreateQueue(queue) + + log.Printf("Inserted %s", insertedId) fmt.Fprintf(w, "Post queue") } diff --git a/internal/models/api/common.go b/internal/models/api/common.go index 48651c0..33a218a 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -1,10 +1,10 @@ package api type ValidationError struct { - Field string - Message string + Field string + Message string } type Validator interface { - Validate() (ValidationError, bool) + Validate() (ValidationError, bool) } diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index 6aec88b..faf3122 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -1,13 +1,13 @@ package api import ( - "fmt" + "fmt" - "github.com/SimplQ/simplQ-golang/internal/models/db" + "github.com/SimplQ/simplQ-golang/internal/models/db" ) type CreateQueueRequest struct { - QueueName string + QueueName string } type CreateQueueResponse db.Queue @@ -16,13 +16,13 @@ const MIN_LENGTH = 4 const MAX_LENGTH = 20 func (req CreateQueueRequest) Validate() (ValidationError, bool) { - if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { - message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) - return ValidationError { - Field: "QueueName", - Message: message, - }, false - } + if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { + message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) + return ValidationError{ + Field: "QueueName", + Message: message, + }, false + } - return ValidationError{}, true + return ValidationError{}, true } From e304ac34ae281093569625afbe8712b1c0918cbe Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Tue, 18 Jan 2022 11:43:45 +0530 Subject: [PATCH 06/12] added documentation for models/api --- internal/models/api/common.go | 11 +++++++++++ internal/models/api/queue.go | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/internal/models/api/common.go b/internal/models/api/common.go index 33a218a..1095324 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -1,10 +1,21 @@ +// Package internal/models/api defines models to be used for API requests package api +// Structure ValidationError is used to describe an error that occurs in a +// validation +// Field - name of the field which had an error +// Message - description of the error type ValidationError struct { Field string Message string } +// Interface Validator defines how to define a validator for a request type Validator interface { + // Method Validate validates the request data + // Returns ValidationError, false if the request data is invalid + // Returns ValidationError, true if the request data is valid + // If ValidationError, true is returned ValidationError is empty and + // should be ignored Validate() (ValidationError, bool) } diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index faf3122..d526c65 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -1,3 +1,4 @@ +// Package internal/models/api defines models to be used for API requests package api import ( @@ -6,15 +7,23 @@ import ( "github.com/SimplQ/simplQ-golang/internal/models/db" ) +// CreateQueueRequest is a model to structure a create queue request +// +// QueueName - Name of the queue to be created type CreateQueueRequest struct { QueueName string } +// CreateQueueResponse is a model to strcuture the response of a create queue +// request type CreateQueueResponse db.Queue +// Minimum and Maximum length of a queue name const MIN_LENGTH = 4 const MAX_LENGTH = 20 +// Validate function for CreateQueueRequest validates if the queue name is within +// the defined range func (req CreateQueueRequest) Validate() (ValidationError, bool) { if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) From f9c54b526cf60e137fa2b201213cd1e94c2e76e2 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 18 Jan 2022 06:14:06 +0000 Subject: [PATCH 07/12] Fix code style issues with gofmt --- internal/models/api/common.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/models/api/common.go b/internal/models/api/common.go index 1095324..d3274a3 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -12,10 +12,10 @@ type ValidationError struct { // Interface Validator defines how to define a validator for a request type Validator interface { - // Method Validate validates the request data - // Returns ValidationError, false if the request data is invalid - // Returns ValidationError, true if the request data is valid - // If ValidationError, true is returned ValidationError is empty and - // should be ignored + // Method Validate validates the request data + // Returns ValidationError, false if the request data is invalid + // Returns ValidationError, true if the request data is valid + // If ValidationError, true is returned ValidationError is empty and + // should be ignored Validate() (ValidationError, bool) } From b0431b6a93df9a9bb7dd6be4c65420c7f6f5517f Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Thu, 20 Jan 2022 11:33:51 +0530 Subject: [PATCH 08/12] updated documentation --- internal/models/api/common.go | 21 ++++++++++++--------- internal/models/api/queue.go | 15 +++++++++------ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/models/api/common.go b/internal/models/api/common.go index 1095324..bd594bf 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -1,21 +1,24 @@ -// Package internal/models/api defines models to be used for API requests +// Package internal/models/api defines models to be used for API requests. package api // Structure ValidationError is used to describe an error that occurs in a -// validation -// Field - name of the field which had an error -// Message - description of the error +// validation. +// +// Members +// +// Field - name of the field which had an error. +// Message - description of the error. type ValidationError struct { Field string Message string } -// Interface Validator defines how to define a validator for a request +// Interface Validator defines how to define a validator for a request. type Validator interface { - // Method Validate validates the request data - // Returns ValidationError, false if the request data is invalid - // Returns ValidationError, true if the request data is valid + // Method Validate validates the request data. + // Returns ValidationError, false if the request data is invalid. + // Returns ValidationError, true if the request data is valid. // If ValidationError, true is returned ValidationError is empty and - // should be ignored + // should be ignored. Validate() (ValidationError, bool) } diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index d526c65..76656bd 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -1,4 +1,4 @@ -// Package internal/models/api defines models to be used for API requests +// Package internal/models/api defines models to be used for API requests. package api import ( @@ -7,23 +7,26 @@ import ( "github.com/SimplQ/simplQ-golang/internal/models/db" ) -// CreateQueueRequest is a model to structure a create queue request +// CreateQueueRequest is a model to structure a create queue request. // -// QueueName - Name of the queue to be created +// Members +// +// QueueName - Name of the queue to be created. type CreateQueueRequest struct { QueueName string } // CreateQueueResponse is a model to strcuture the response of a create queue -// request +// request. type CreateQueueResponse db.Queue -// Minimum and Maximum length of a queue name +// Minimum length of a queue name. const MIN_LENGTH = 4 +// Maximum length of a queue name. const MAX_LENGTH = 20 // Validate function for CreateQueueRequest validates if the queue name is within -// the defined range +// the defined range. func (req CreateQueueRequest) Validate() (ValidationError, bool) { if len(req.QueueName) < MIN_LENGTH || len(req.QueueName) > MAX_LENGTH { message := fmt.Sprintf("Queue name length should be greater than %d characters and less than %d charaacters", MIN_LENGTH, MAX_LENGTH) From f9ba54513f0e118ade3ac5f0ea2abb507da007ec Mon Sep 17 00:00:00 2001 From: Lint Action Date: Thu, 20 Jan 2022 06:05:50 +0000 Subject: [PATCH 09/12] Fix code style issues with gofmt --- internal/models/api/common.go | 10 +++++----- internal/models/api/queue.go | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/models/api/common.go b/internal/models/api/common.go index bd594bf..30c3f2b 100644 --- a/internal/models/api/common.go +++ b/internal/models/api/common.go @@ -15,10 +15,10 @@ type ValidationError struct { // Interface Validator defines how to define a validator for a request. type Validator interface { - // Method Validate validates the request data. - // Returns ValidationError, false if the request data is invalid. - // Returns ValidationError, true if the request data is valid. - // If ValidationError, true is returned ValidationError is empty and - // should be ignored. + // Method Validate validates the request data. + // Returns ValidationError, false if the request data is invalid. + // Returns ValidationError, true if the request data is valid. + // If ValidationError, true is returned ValidationError is empty and + // should be ignored. Validate() (ValidationError, bool) } diff --git a/internal/models/api/queue.go b/internal/models/api/queue.go index 76656bd..2b5e34d 100644 --- a/internal/models/api/queue.go +++ b/internal/models/api/queue.go @@ -22,6 +22,7 @@ type CreateQueueResponse db.Queue // Minimum length of a queue name. const MIN_LENGTH = 4 + // Maximum length of a queue name. const MAX_LENGTH = 20 From 64db4572a087ec4d3456607fdc78783cd9b2bf71 Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Tue, 1 Feb 2022 13:20:49 +0530 Subject: [PATCH 10/12] removed unused code --- internal/models/common.go | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 internal/models/common.go diff --git a/internal/models/common.go b/internal/models/common.go deleted file mode 100644 index 02fb360..0000000 --- a/internal/models/common.go +++ /dev/null @@ -1,3 +0,0 @@ -package models - -type Id string From 449084254edb629aba8692648d0329d92194ee26 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Fri, 4 Feb 2022 04:29:45 +0000 Subject: [PATCH 11/12] Fix code style issues with gofmt --- internal/handler/queue.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/handler/queue.go b/internal/handler/queue.go index 9ad3813..1ba9513 100644 --- a/internal/handler/queue.go +++ b/internal/handler/queue.go @@ -65,8 +65,8 @@ func CreateQueue(w http.ResponseWriter, r *http.Request) { http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) return } - - log.Printf("Inserted %s", insertedId) + + log.Printf("Inserted %s", insertedId) w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, "Queue created with Id: %s", insertedId) } From 2a24defdb2634574f8ce18ed1122d6d1dcba4f3c Mon Sep 17 00:00:00 2001 From: aneeshsharma Date: Wed, 9 Feb 2022 09:39:54 +0530 Subject: [PATCH 12/12] comment --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index f3f7fc9..cb18d0f 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ LD_FLAGS='-extldflags "-static"' all: build/azure_functions.zip build/azure_functions.zip: AzureFunctions/handler + # Create directory for build mkdir -p build cd AzureFunctions && zip -r ../build/azure_functions.zip * # Display size of output zip