Skip to content
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
24 changes: 24 additions & 0 deletions src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"

"github.com/gorilla/mux"
"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/repository"
"github.com/switcherapi/switcher-gitops/src/utils"
Expand All @@ -26,6 +27,17 @@ func NewAccountController(repo repository.AccountRepository) *AccountController
}
}

func (controller *AccountController) RegisterRoutes(r *mux.Router) http.Handler {
const routesDomainVar = "/{domainId}"

r.HandleFunc(controller.RouteAccountPath, controller.CreateAccountHandler).Methods(http.MethodPost)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.FetchAccountHandler).Methods(http.MethodGet)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.UpdateAccountHandler).Methods(http.MethodPut)
r.HandleFunc(controller.RouteAccountPath+routesDomainVar, controller.DeleteAccountHandler).Methods(http.MethodDelete)

return r
}

func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter, r *http.Request) {
var accountRequest model.Account
err := json.NewDecoder(r.Body).Decode(&accountRequest)
Expand Down Expand Up @@ -74,3 +86,15 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,

utils.ResponseJSON(w, accountUpdated, http.StatusOK)
}

func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
domainId := r.URL.Path[len(controller.RouteAccountPath+"/"):]
err := controller.AccountRepository.DeleteByDomainId(domainId)
if err != nil {
log.Println(err)
utils.ResponseJSON(w, ErrorResponse{Error: "Error deleting account: " + err.Error()}, http.StatusInternalServerError)
return
}

utils.ResponseJSON(w, nil, http.StatusNoContent)
}
36 changes: 32 additions & 4 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestCreateAccountHandler(t *testing.T) {
func TestCreateAccountHandlerInvalidRequest(t *testing.T) {
// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", accountController.RouteAccountPath, nil)
r := httptest.NewRequest(http.MethodPost, accountController.RouteAccountPath, nil)

// Test
accountController.CreateAccountHandler(w, r)
Expand All @@ -47,7 +47,7 @@ func TestFetchAccountHandlerByDomainId(t *testing.T) {

// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", accountController.RouteAccountPath+"/123", nil)
r := httptest.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/123", nil)

// Test
accountController.FetchAccountHandler(w, r)
Expand All @@ -64,7 +64,7 @@ func TestFetchAccountHandlerByDomainId(t *testing.T) {
func TestFetchAccountHandlerByDomainIdNotFound(t *testing.T) {
// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", accountController.RouteAccountPath+"/111", nil)
r := httptest.NewRequest(http.MethodGet, accountController.RouteAccountPath+"/111", nil)

// Test
accountController.FetchAccountHandler(w, r)
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestUpdateAccountHandler(t *testing.T) {
func TestUpdateAccountHandlerInvalidRequest(t *testing.T) {
// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest("PUT", accountController.RouteAccountPath, nil)
r := httptest.NewRequest(http.MethodPut, accountController.RouteAccountPath, nil)

// Test
accountController.UpdateAccountHandler(w, r)
Expand All @@ -106,6 +106,34 @@ func TestUpdateAccountHandlerInvalidRequest(t *testing.T) {
assert.Equal(t, "{\"error\":\"Invalid request\"}", w.Body.String())
}

func TestDeleteAccountHandler(t *testing.T) {
// Create an account
accountController.CreateAccountHandler(givenAccountRequest(accountV1))

// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/123", nil)

// Test
accountController.DeleteAccountHandler(w, r)

// Assert
assert.Equal(t, http.StatusNoContent, w.Code)
}

func TestDeleteAccountHandlerNotFound(t *testing.T) {
// Create a request and response recorder
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodDelete, accountController.RouteAccountPath+"/111", nil)

// Test
accountController.DeleteAccountHandler(w, r)

// Assert
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "{\"error\":\"Error deleting account: Account not found for domain.id: 111\"}", w.Body.String())
}

// Helpers

func givenAccountRequest(data model.Account) (*httptest.ResponseRecorder, *http.Request) {
Expand Down
7 changes: 7 additions & 0 deletions src/controller/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controller
import (
"net/http"

"github.com/gorilla/mux"
"github.com/switcherapi/switcher-gitops/src/utils"
)

Expand All @@ -20,6 +21,12 @@ func NewApiController() *ApiController {
}
}

func (controller *ApiController) RegisterRoutes(r *mux.Router) http.Handler {
r.HandleFunc(controller.RouteCheckApiPath, controller.CheckApiHandler).Methods(http.MethodGet)

return r
}

func (controller *ApiController) CheckApiHandler(w http.ResponseWriter, r *http.Request) {
utils.ResponseJSON(w, ApiCheckResponse{Message: "API is working"}, http.StatusOK)
}
29 changes: 27 additions & 2 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ type AccountRepository interface {
Create(account *model.Account) (*model.Account, error)
FetchByDomainId(domainId string) (*model.Account, error)
Update(account *model.Account) (*model.Account, error)
DeleteByDomainId(domainId string) error
}

type AccountRepositoryMongo struct {
Db *mongo.Database
}

type ErrAccountNotFound struct {
DomainId string
}

func (err ErrAccountNotFound) Error() string {
return "Account not found for domain.id: " + err.DomainId
}

const domainIdFilter = "domain.id"

func (repo *AccountRepositoryMongo) Create(account *model.Account) (*model.Account, error) {
collection, ctx, cancel := getDbContext(repo)
defer cancel()
Expand All @@ -37,7 +48,7 @@ func (repo *AccountRepositoryMongo) FetchByDomainId(domainId string) (*model.Acc
defer cancel()

var account model.Account
filter := primitive.M{"domain.id": domainId}
filter := primitive.M{domainIdFilter: domainId}
err := collection.FindOne(ctx, filter).Decode(&account)
if err != nil {
return nil, err
Expand All @@ -50,7 +61,7 @@ func (repo *AccountRepositoryMongo) Update(account *model.Account) (*model.Accou
collection, ctx, cancel := getDbContext(repo)
defer cancel()

filter := primitive.M{"domain.id": account.Domain.ID}
filter := primitive.M{domainIdFilter: account.Domain.ID}
update := primitive.M{
"$set": account,
}
Expand All @@ -63,6 +74,20 @@ func (repo *AccountRepositoryMongo) Update(account *model.Account) (*model.Accou
return account, nil
}

func (repo *AccountRepositoryMongo) DeleteByDomainId(domainId string) error {
collection, ctx, cancel := getDbContext(repo)
defer cancel()

filter := primitive.M{domainIdFilter: domainId}
result, err := collection.DeleteOne(ctx, filter)

if result.DeletedCount == 0 {
return ErrAccountNotFound{DomainId: domainId}
}

return err
}

func getDbContext(repo *AccountRepositoryMongo) (*mongo.Collection, context.Context, context.CancelFunc) {
collection := repo.Db.Collection(model.CollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down
6 changes: 2 additions & 4 deletions src/server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ func initRoutes(db *mongo.Database) *mux.Router {
accountController := controller.NewAccountController(&repository.AccountRepositoryMongo{Db: db})

r := mux.NewRouter()
r.HandleFunc(apiController.RouteCheckApiPath, apiController.CheckApiHandler).Methods("GET")
r.HandleFunc(accountController.RouteAccountPath, accountController.CreateAccountHandler).Methods("POST")
r.HandleFunc(accountController.RouteAccountPath+"/{domainId}", accountController.FetchAccountHandler).Methods("GET")
r.HandleFunc(accountController.RouteAccountPath+"/{domainId}", accountController.UpdateAccountHandler).Methods("PUT")
apiController.RegisterRoutes(r)
accountController.RegisterRoutes(r)

return r
}