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
86 changes: 86 additions & 0 deletions _bruno/Flows/Create Flow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
info:
name: Create Flow
type: http
seq: 1

http:
method: POST
url: "{{baseUrl}}/api/products/:id/flows"
params:
- name: id
value: "2"
type: path
body:
type: json
data: |-
{
"name": "Flow 1",
"description": "flow 1 desc"
}
auth: inherit

runtime:
assertions:
- expression: res.status
operator: eq
value: "201"

settings:
encodeUrl: true
timeout: 0
followRedirects: true
maxRedirects: 5

examples:
- name: sample
description: sample successful call to create a product flow
request:
url: "{{baseUrl}}/api/products/:id/flows"
method: POST
params:
- name: id
value: "2"
type: path
body:
type: json
data: |-
{
"name": "Flow 1",
"description": "flow 1 desc"
}
response:
status: 201
statusText: Created
headers:
- name: content-type
value: application/json
- name: vary
value: Origin, Accept-Encoding
- name: date
value: Sat, 16 May 2026 00:11:09 GMT
- name: content-length
value: "134"
body:
type: json
data: |-
{
"id": 1,
"product_id": 2,
"name": "Flow 1",
"description": "flow 1 desc",
"created_at": "2026-05-15T19:11:09.35732-05:00",
"updated_at": "2026-05-15T19:11:09.35732-05:00"
}

docs: |-
# Create Flow

Creates a new flow for a specific product. The product ID is passed as a path parameter.

## Possible Status Codes
| Status Code | Notes |
| -- | --|
| 201 | Created |
| 400 | Invalid product ID or request body |
| 404 | Product not found |
| 500 | Internal server error |
7 changes: 7 additions & 0 deletions _bruno/Flows/folder.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
info:
name: Flows
type: folder
seq: 3

request:
auth: inherit
5 changes: 3 additions & 2 deletions internal/flow/flows.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- name: CreateFlow :execrows
-- name: CreateFlow :one
INSERT INTO flows(product_id, name, description, created_at, updated_at)
VALUES(@product_id, @name, @description, @time_stamp, @time_stamp);
VALUES(@product_id, @name, @description, @time_stamp, @time_stamp)
RETURNING *;

-- name: GetFlowsByProduct :many
SELECT id, name, description, created_at, updated_at FROM flows WHERE product_id = @product_id;
Expand Down
21 changes: 14 additions & 7 deletions internal/flow/flows.sql.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions internal/flow/handler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package flow

import (
"bytes"
"context"
"encoding/json"
"errors"
"log/slog"
"net/http"
"products/internal"
"strings"
"time"

"github.com/jackc/pgx/v5"
)

func NewHandler(db DBTX) Handler {
queries := &Queries{
db: db,
Expand All @@ -12,3 +26,45 @@ func NewHandler(db DBTX) Handler {
type flowHandler struct {
queries Querier
}

func (h *flowHandler) CreateFlow(w http.ResponseWriter, r *http.Request) {
id, ok := internal.GetIntFromRequestPath("id", r)
if !ok {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Invalid product ID"}, http.StatusBadRequest)
return
}
req := &createFlowRequest{}
if err := json.NewDecoder(r.Body).Decode(req); err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Invalid request body"}, http.StatusBadRequest)
return
}
if strings.TrimSpace(req.Name) == "" {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Name is required"}, http.StatusBadRequest)
return
}
contextWithTimeOut, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
flow, err := h.queries.CreateFlow(contextWithTimeOut, req.ToParams(id))
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Product not found"}, http.StatusNotFound)
return
}
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Failed to create flow"}, http.StatusInternalServerError)
return
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(flow)
if err != nil {
internal.HandleHttpError(w, internal.ErrorEnvelope{Detail: "Failed to encode response"}, http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
_, err = w.Write(buf.Bytes())
if err != nil {
slog.Error("Failed to write response", "request", r.URL.Path, "error", err)
}
}
Loading
Loading