Skip to content

Commit

Permalink
used const and var for string variables (#102)
Browse files Browse the repository at this point in the history
* used const and var for strings

* update

* update
  • Loading branch information
recep authored May 28, 2020
1 parent c7ed7ff commit 71cabaf
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 61 deletions.
27 changes: 17 additions & 10 deletions internal/api/auth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import (
"github.com/spf13/viper"
)

var (
InvalidUser = "Invalid user"
ValidToken = "Token is valid"
InvalidToken = "Token is expired or not valid!"
TokenCreateErr = "Token could not be created"
)

// Signin ...
func Signin(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -25,7 +32,7 @@ func Signin(s storage.Store) http.HandlerFunc {
// get loginDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&loginDTO); err != nil {
RespondWithError(w, http.StatusUnprocessableEntity, "Invalid json provided")
RespondWithError(w, http.StatusUnprocessableEntity, InvalidJSON)
return
}
defer r.Body.Close()
Expand All @@ -34,21 +41,21 @@ func Signin(s storage.Store) http.HandlerFunc {
validateError := validate.Struct(loginDTO)
if validateError != nil {
errs := GetErrors(validateError.(validator.ValidationErrors))
RespondWithErrors(w, http.StatusBadRequest, "Invalid resquest payload", errs)
RespondWithErrors(w, http.StatusBadRequest, InvalidRequestPayload, errs)
return
}

// check user
if viper.GetString("server.username") != loginDTO.Username ||
viper.GetString("server.password") != loginDTO.Password {
RespondWithError(w, http.StatusUnauthorized, "Invalid User")
RespondWithError(w, http.StatusUnauthorized, InvalidUser)
return
}

//create token
token, err := app.CreateToken()
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Token could not be created")
RespondWithError(w, http.StatusInternalServerError, TokenCreateErr)
return
}

Expand Down Expand Up @@ -77,7 +84,7 @@ func RefreshToken(s storage.Store) http.HandlerFunc {
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&mapToken); err != nil {
errs := []string{"REFRESH_TOKEN_ERROR"}
RespondWithErrors(w, http.StatusUnprocessableEntity, "Invalid json provided", errs)
RespondWithErrors(w, http.StatusUnprocessableEntity, InvalidJSON, errs)
return
}
defer r.Body.Close()
Expand All @@ -101,14 +108,14 @@ func RefreshToken(s storage.Store) http.HandlerFunc {
if !s.Tokens().Any(uuid) {
userid, _ := strconv.Atoi(fmt.Sprintf("%.f", claims["user_id"]))
s.Tokens().Delete(userid)
RespondWithError(w, http.StatusUnauthorized, "Invalid token")
RespondWithError(w, http.StatusUnauthorized, InvalidToken)
return
}

//create token
newtoken, err := app.CreateToken()
if err != nil {
RespondWithError(w, http.StatusInternalServerError, "Token could not be created")
RespondWithError(w, http.StatusInternalServerError, TokenCreateErr)
return
}

Expand Down Expand Up @@ -141,14 +148,14 @@ func CheckToken(w http.ResponseWriter, r *http.Request) {

_, err := app.TokenValid(token)
if err != nil {
RespondWithError(w, http.StatusUnauthorized, "Token is expired or not valid!")
RespondWithError(w, http.StatusUnauthorized, InvalidToken)
return
}

response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "Token is valid!",
Status: Success,
Message: ValidToken,
}

RespondWithJSON(w, http.StatusOK, response)
Expand Down
14 changes: 9 additions & 5 deletions internal/api/bank_account_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import (
"github.com/gorilla/mux"
)

const (
BankAccountDeleteSuccess = "BankAccount deleted successfully!"
)

// FindAll ...
func FindAllBankAccounts(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
bankAccounts := []model.BankAccount{}
var bankAccounts []model.BankAccount

fields := []string{"id", "created_at", "updated_at", "bank_name", "bank_code", "account_name", "account_number", "iban", "currency"}
argsStr, argsInt := SetArgs(r, fields)
Expand Down Expand Up @@ -65,7 +69,7 @@ func CreateBankAccount(s storage.Store) http.HandlerFunc {

decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&bankAccountDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -93,7 +97,7 @@ func UpdateBankAccount(s storage.Store) http.HandlerFunc {
var bankAccountDTO model.BankAccountDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&bankAccountDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -138,8 +142,8 @@ func DeleteBankAccount(s storage.Store) http.HandlerFunc {

response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "BankAccount deleted successfully!",
Status: Success,
Message: BankAccountDeleteSuccess,
}
RespondWithJSON(w, http.StatusOK, response)
}
Expand Down
16 changes: 11 additions & 5 deletions internal/api/credit_card_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import (
"github.com/gorilla/mux"
)

const (
InvalidRequestPayload = "Invalid request payload"
CreditCardDeleted = "CreditCard deleted successfully!"
Success = "Success"
)

// FindAll ...
func FindAllCreditCards(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
creditCards := []model.CreditCard{}
var creditCards []model.CreditCard

fields := []string{"id", "created_at", "updated_at", "bank_name", "bank_code", "account_name", "account_number", "iban", "currency"}
argsStr, argsInt := SetArgs(r, fields)
Expand Down Expand Up @@ -66,7 +72,7 @@ func CreateCreditCard(s storage.Store) http.HandlerFunc {

decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&creditCardDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -94,7 +100,7 @@ func UpdateCreditCard(s storage.Store) http.HandlerFunc {
var creditCardDTO model.CreditCardDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&creditCardDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -139,8 +145,8 @@ func DeleteCreditCard(s storage.Store) http.HandlerFunc {

response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "CreditCard deleted successfully!",
Status: Success,
Message: CreditCardDeleted,
}
RespondWithJSON(w, http.StatusOK, response)
}
Expand Down
20 changes: 12 additions & 8 deletions internal/api/login_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@ import (
"github.com/gorilla/mux"
)

const (
LoginDeleteSuccess = "Login deleted successfully!"
)

// FindAll ...
func FindAllLogins(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var err error
logins := []model.Login{}
var loginList []model.Login

fields := []string{"id", "created_at", "updated_at", "url", "username"}
argsStr, argsInt := SetArgs(r, fields)

logins, err = s.Logins().FindAll(argsStr, argsInt)
loginList, err = s.Logins().FindAll(argsStr, argsInt)

if err != nil {
RespondWithError(w, http.StatusNotFound, err.Error())
return
}

logins = app.DecryptLoginPasswords(logins)
RespondWithJSON(w, http.StatusOK, logins)
loginList = app.DecryptLoginPasswords(loginList)
RespondWithJSON(w, http.StatusOK, loginList)
}
}

Expand Down Expand Up @@ -66,7 +70,7 @@ func CreateLogin(s storage.Store) http.HandlerFunc {

decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&loginDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -98,7 +102,7 @@ func UpdateLogin(s storage.Store) http.HandlerFunc {
var loginDTO model.LoginDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&loginDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -143,8 +147,8 @@ func DeleteLogin(s storage.Store) http.HandlerFunc {

response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "Login deleted successfully!",
Status: Success,
Message: LoginDeleteSuccess,
}
RespondWithJSON(w, http.StatusOK, response)
}
Expand Down
12 changes: 8 additions & 4 deletions internal/api/note_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/gorilla/mux"
)

const (
NoteDeleteSuccess = "Note deleted successfully!"
)

// FindAll ...
func FindAllNotes(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -66,7 +70,7 @@ func CreateNote(s storage.Store) http.HandlerFunc {

decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&noteDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -94,7 +98,7 @@ func UpdateNote(s storage.Store) http.HandlerFunc {
var noteDTO model.NoteDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&noteDTO); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -139,8 +143,8 @@ func DeleteNote(s storage.Store) http.HandlerFunc {

response := model.Response{
Code: http.StatusOK,
Status: "Success",
Message: "Note deleted successfully!",
Status: Success,
Message: NoteDeleteSuccess,
}
RespondWithJSON(w, http.StatusOK, response)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/passwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func FindSamePassword(s storage.Store) http.HandlerFunc {

decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&password); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid resquest payload")
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
Expand All @@ -37,7 +37,7 @@ func GeneratePassword(w http.ResponseWriter, r *http.Request) {
password := app.Password()
response := model.Response{
Code: http.StatusOK,
Status: "Success",
Status: Success,
Message: password,
}
RespondWithJSON(w, http.StatusOK, response)
Expand Down
27 changes: 17 additions & 10 deletions internal/api/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
"github.com/spf13/viper"
)

const (
InvalidJSON = "Invalid json provided"
RestoreBackupSuccess = "Restore from backup completed successfully!"
ImportSuccess = "Import finished successfully!"
BackupSuccess = "Backup completed successfully!"
)

// Import ...
func Import(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -49,7 +56,7 @@ func Import(s storage.Store) http.HandlerFunc {
return
}

response := model.Response{http.StatusOK, "Success", "Import finished successfully!"}
response := model.Response{http.StatusOK, Success, ImportSuccess}
RespondWithJSON(w, http.StatusOK, response)
}
}
Expand All @@ -58,15 +65,15 @@ func Import(s storage.Store) http.HandlerFunc {
func Export(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

var logins []model.Login
s.Find(&logins)
var loginList []model.Login
s.Find(&loginList)

logins = app.DecryptLoginPasswords(logins)
loginList = app.DecryptLoginPasswords(loginList)

content := [][]string{}
var content [][]string
content = append(content, []string{"URL", "Username", "Password"})
for i := range logins {
content = append(content, []string{logins[i].URL, logins[i].Username, logins[i].Password})
for i := range loginList {
content = append(content, []string{loginList[i].URL, loginList[i].Username, loginList[i].Password})
}

b := &bytes.Buffer{} // creates IO Writer
Expand All @@ -89,7 +96,7 @@ func Restore(s storage.Store) http.HandlerFunc {
// get restoreDTO
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&restoreDTO); err != nil {
RespondWithError(w, http.StatusUnprocessableEntity, "Invalid json provided")
RespondWithError(w, http.StatusUnprocessableEntity, InvalidJSON)
return
}
defer r.Body.Close()
Expand Down Expand Up @@ -124,7 +131,7 @@ func Restore(s storage.Store) http.HandlerFunc {
s.Logins().Save(login)
}

response := model.Response{http.StatusOK, "Success", "Restore from backup completed successfully!"}
response := model.Response{http.StatusOK, Success, RestoreBackupSuccess}
RespondWithJSON(w, http.StatusOK, response)
}
}
Expand All @@ -139,7 +146,7 @@ func Backup(s storage.Store) http.HandlerFunc {
return
}

response := model.Response{http.StatusOK, "Success", "Backup completed successfully!"}
response := model.Response{http.StatusOK, Success, BackupSuccess}
RespondWithJSON(w, http.StatusOK, response)
}
}
Expand Down
Loading

0 comments on commit 71cabaf

Please sign in to comment.