Commit 752168f2 authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Refactor : reduce code complexity

parent ecf20102
Pipeline #5300 passed with stages
in 3 minutes and 25 seconds
......@@ -31,7 +31,7 @@ func (d *DataHandler) HandleBankAccounts(w http.ResponseWriter, r *http.Request)
case "BANKER":
d.postBankAccountBanker(w, r, id)
case "CLIENT":
d.postBankAccountClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -42,7 +42,7 @@ func (d *DataHandler) HandleBankAccounts(w http.ResponseWriter, r *http.Request)
case "BANKER":
d.putBankAccountBanker(w, r, id)
case "CLIENT":
d.putBankAccountClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -53,7 +53,7 @@ func (d *DataHandler) HandleBankAccounts(w http.ResponseWriter, r *http.Request)
case "BANKER":
d.deleteBankAccountBanker(w, r, id)
case "CLIENT":
d.deleteBankAccountClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -152,10 +152,6 @@ func (d *DataHandler) postBankAccountBanker(w http.ResponseWriter, r *http.Reque
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) postBankAccountClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) putBankAccountAdmin(w http.ResponseWriter, r *http.Request, id int) {
var o BankAccount
if err := d.db.Preload("Operations").First(&o, id).Error; err != nil {
......@@ -202,10 +198,6 @@ func (d *DataHandler) putBankAccountBanker(w http.ResponseWriter, r *http.Reques
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) putBankAccountClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) deleteBankAccountAdmin(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o BankAccount
......@@ -237,7 +229,3 @@ func (d *DataHandler) deleteBankAccountBanker(w http.ResponseWriter, r *http.Req
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
func (d *DataHandler) deleteBankAccountClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
......@@ -29,7 +29,7 @@ func (d *DataHandler) HandleBankers(w http.ResponseWriter, r *http.Request) {
case "ADMIN":
d.postBankerAdmin(w, r, id)
case "BANKER", "CLIENT":
d.postBankerClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -39,7 +39,7 @@ func (d *DataHandler) HandleBankers(w http.ResponseWriter, r *http.Request) {
case "ADMIN":
d.putBankerAdmin(w, r, id)
case "BANKER", "CLIENT":
d.putBankerClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -48,7 +48,7 @@ func (d *DataHandler) HandleBankers(w http.ResponseWriter, r *http.Request) {
case "ADMIN":
d.deleteBankerAdmin(w, r, id)
case "BANKER", "CLIENT":
d.deleteBankerClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -132,10 +132,6 @@ func (d *DataHandler) postBankerAdmin(w http.ResponseWriter, r *http.Request, id
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) postBankerClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) putBankerAdmin(w http.ResponseWriter, r *http.Request, id int) {
var o UserBanker
if err := d.db.Preload("UserClients").Where(reqID, id).First(&o).Error; err != nil {
......@@ -152,10 +148,6 @@ func (d *DataHandler) putBankerAdmin(w http.ResponseWriter, r *http.Request, id
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) putBankerClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) deleteBankerAdmin(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o UserBanker
......@@ -168,7 +160,3 @@ func (d *DataHandler) deleteBankerAdmin(w http.ResponseWriter, r *http.Request,
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
func (d *DataHandler) deleteBankerClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
......@@ -32,7 +32,7 @@ func (d *DataHandler) HandleClients(w http.ResponseWriter, r *http.Request) {
case "BANKER":
d.postClientBanker(w, r, id)
case "CLIENT":
d.postClientClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -43,7 +43,7 @@ func (d *DataHandler) HandleClients(w http.ResponseWriter, r *http.Request) {
case "BANKER":
d.putClientBanker(w, r, id)
case "CLIENT":
d.putClientClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -54,7 +54,7 @@ func (d *DataHandler) HandleClients(w http.ResponseWriter, r *http.Request) {
case "BANKER":
d.deleteClientBanker(w, r, id)
case "CLIENT":
d.deleteClientClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -152,10 +152,6 @@ func (d *DataHandler) postClientBanker(w http.ResponseWriter, r *http.Request, i
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) postClientClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) putClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
var o UserClient
if err := d.db.Preload("BankAccounts").Where(reqID, id).First(&o).Error; err != nil {
......@@ -191,10 +187,6 @@ func (d *DataHandler) putClientBanker(w http.ResponseWriter, r *http.Request, id
json.NewEncoder(w).Encode(o)
}
func (d *DataHandler) putClientClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
func (d *DataHandler) deleteClientAdmin(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o UserClient
......@@ -231,7 +223,3 @@ func (d *DataHandler) deleteClientBanker(w http.ResponseWriter, r *http.Request,
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
func (d *DataHandler) deleteClientClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
......@@ -17,19 +17,19 @@ type DataHandler struct {
db *gorm.DB
}
// ErrorIDDoesNotExist = "id does not exist"
// ErrorIDDoesNotExist = "id does not exist" with 404 http.StatusNotFound
const ErrorIDDoesNotExist = "id does not exist"
// ErrorIDIsMissing = "id is missing"
// ErrorIDIsMissing = "id is missing" with 404 http.StatusNotFound
const ErrorIDIsMissing = "id is missing"
// ErrorCannotAccessRessource = "You can not access this ressource"
// ErrorCannotAccessRessource = "You can not access this ressource" with 403 http.StatusForbidden
const ErrorCannotAccessRessource = "You can not access this ressource"
// ErrorRoleOfLoggedUser = "Could not get role of logged user"
// ErrorRoleOfLoggedUser = "Could not get role of logged user" with 500 http.StatusInternalServerError
const ErrorRoleOfLoggedUser = "Could not get role of logged user"
// ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
// ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource." with 405 http.StatusMethodNotAllowed
const ErrorNotAuthorizeMethodOnRessource = "You're not authorize to execute this method on this ressource."
// ErrorUserIDIsMissing = "id of UserClient is missing"
......
......@@ -34,7 +34,7 @@ func (d *DataHandler) HandleOperations(w http.ResponseWriter, r *http.Request) {
case "ADMIN", "BANKER":
d.deleteOperationBanker(w, r, id)
case "CLIENT":
d.deleteOperationClient(w, r, id)
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
default:
http.Error(w, ErrorRoleOfLoggedUser, http.StatusInternalServerError)
}
......@@ -133,7 +133,3 @@ func (d *DataHandler) deleteOperationBanker(w http.ResponseWriter, r *http.Reque
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
}
}
func (d *DataHandler) deleteOperationClient(w http.ResponseWriter, r *http.Request, id int) {
http.Error(w, ErrorNotAuthorizeMethodOnRessource, http.StatusMethodNotAllowed)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment