diff --git a/Makefile b/Makefile index 2be71f6..ed49015 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ publish: ## Deploy ondehoje service on heroku .PHONY: heroku/release heroku/release: - heroku container:release web --app ondehoje + heroku container:release web --app ondehoje ${image} ## Run ondehoje service .PHONY: run diff --git a/README.md b/README.md index 95fbda7..61e76fc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Onde Hoje ?

-

A Single place to share the underground

+

Uncovering the Underground

Onde Hoje is an innovative application that allows users to share underground events in their city. This app is designed for people who are looking for unique experiences and want to explore the hidden gems of their city. @@ -56,7 +56,11 @@ sudo apt-get install postgresql #to install psql heroku pg:psql -a ondehoje # to access database and execute admin commands ``` - +## Local Database +Pass: example_password +```bash +psql -h localhost -p 5432 -d example_db -U postgres +``` # Ship New code to production Although there is a CI for that, it's possible to SHIP🚀 new code to production just using your machine. @@ -70,6 +74,12 @@ make heroku/release ``` +# Load Test + +``` +k6 run --vus 10 --duration 30s load-test/create-event.js +``` + # Useful Vscode extensions -[OpenAPI Swagger Editor](https://42crunch.com/tutorial-openapi-swagger-extension-vs-code/) \ No newline at end of file +[OpenAPI Swagger Editor](https://42crunch.com/tutorial-openapi-swagger-extension-vs-code/) diff --git a/api/main.go b/api/main.go index 2ba6d1c..05e2ccc 100644 --- a/api/main.go +++ b/api/main.go @@ -18,6 +18,27 @@ const ( eventPathId = "/event/{id}" ) +func deleteAllEventsHandler(eventRepo event.Repository) http.HandlerFunc { + fn := func(w http.ResponseWriter, r *http.Request) { + slog.Info("Calling deleteAll") + if r.Method != http.MethodDelete { + slog.Error("Method not allowed") + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + slog.Info("Deleting all events") + err := eventRepo.DeleteAll(r.Context()) + if err != nil { + slog.Error("Delete failed", "error", err) + http.Error(w, "Delete failed", http.StatusInternalServerError) + return + } + slog.Info("Events deleted successfully") + } + return http.HandlerFunc(fn) +} + func deleteEventHandler(eventRepo event.Repository) http.HandlerFunc { fn := func(w http.ResponseWriter, r *http.Request) { slog.Info("Calling deleteEventHandler") @@ -222,6 +243,7 @@ func HandlerFactory(db *pgxpool.Pool) http.Handler { router.HandleFunc(eventPathId, deleteEventHandler(eventSQLRepo)).Methods(http.MethodDelete) router.HandleFunc(eventPathId, getByIDHandler(eventSQLRepo)).Methods(http.MethodGet) router.HandleFunc(eventPathId, Update(eventSQLRepo)).Methods(http.MethodPut) + router.HandleFunc(eventPath, deleteAllEventsHandler(eventSQLRepo)).Methods(http.MethodDelete) // this function will not be exposed in the swagger // documentation for developers opts := middleware.SwaggerUIOpts{SpecURL: "openapi.yaml"} sh := middleware.SwaggerUI(opts, nil) diff --git a/event/event.go b/event/event.go index c80ed90..ec3e631 100644 --- a/event/event.go +++ b/event/event.go @@ -31,6 +31,7 @@ type Repository interface { All(ctx context.Context) ([]Event, error) GetByID(ctx context.Context, id int64) (*Event, error) Update(ctx context.Context, id int64, newEvent Event) (*Event, error) + DeleteAll(ctx context.Context) error } type SQLRepository struct { @@ -41,6 +42,14 @@ func EventSQLRepository(db *pgxpool.Pool) *SQLRepository { return &SQLRepository{db: db} } +func (r *SQLRepository) DeleteAll(ctx context.Context) error { + _, err := r.db.Exec(ctx, `DELETE FROM events`) + if err != nil { + return err + } + return nil +} + func (r *SQLRepository) Update(ctx context.Context, id int64, newEvent Event) (*Event, error) { err := r.db.QueryRow(ctx, diff --git a/load-test/create-event.js b/load-test/create-event.js new file mode 100644 index 0000000..915f895 --- /dev/null +++ b/load-test/create-event.js @@ -0,0 +1,41 @@ +//import k6 and do a request to localhost/events with a POST method passing the event object as a JSON string +import http from 'k6/http'; +import { sleep, check } from 'k6'; + + +export function teardown() { + //delte all events created by the load test + const url = 'http://localhost:8000/event'; + const params = { + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + }, + } + let res = http.del(url, null, params) + check(res, { + 'is status 200': (r) => r.status === 200, + }); +} + +export default function () { + const url = 'http://localhost:8000/event'; + const params = { + headers: { + 'Content-Type': 'application/json', + 'accept': '*/*', + }, + } + const payload = JSON.stringify({ + "title": "string", + "description": "string", + "location": "string", + "start_time": "2023-05-07T13:22:08.124Z", + "end_time": "2023-05-07T13:22:08.124Z", + "instagram_page": "string" + }) + let res = http.post(url, payload, params) + check(res, { + 'is status 200': (r) => r.status === 200, + }); +} \ No newline at end of file diff --git a/openapi.yaml b/openapi.yaml index 4ecf5a8..c09fd94 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -27,12 +27,6 @@ paths: summary: Get all events tags: - "Event" - requestBody: - required: true - content: - application/json: - schema: - $ref: "#/components/schemas/EventRequest" responses: "200": description: OK