|
| 1 | +package collections |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/maniac-en/req/internal/database" |
| 9 | +) |
| 10 | + |
| 11 | +func (c *CollectionsManager) CreateEndpoint(ctx context.Context, name, method, url string, collectionId int64) (database.Endpoint, error) { |
| 12 | + return c.DB.CreateEndpoint(ctx, database.CreateEndpointParams{ |
| 13 | + Name: name, |
| 14 | + CollectionID: collectionId, |
| 15 | + Method: method, |
| 16 | + Url: url, |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +func (c *CollectionsManager) GetEndpoints(ctx context.Context, collectionId int64) ([]database.Endpoint, error) { |
| 21 | + return c.DB.ListEndpoints(ctx, collectionId) |
| 22 | +} |
| 23 | + |
| 24 | +func (c *CollectionsManager) GetEndpoint(ctx context.Context, id int64) (database.Endpoint, error) { |
| 25 | + return c.DB.GetEndpoint(ctx, id) |
| 26 | +} |
| 27 | + |
| 28 | +func (c *CollectionsManager) UpdateEndpoint( |
| 29 | + ctx context.Context, |
| 30 | + id int64, |
| 31 | + name string, |
| 32 | + method string, |
| 33 | + url string, |
| 34 | + headers string, |
| 35 | + queryParams string, |
| 36 | + requestBody string, |
| 37 | +) (database.Endpoint, error) { |
| 38 | + existingEndpoint, err := c.DB.GetEndpoint(ctx, id) |
| 39 | + if err != nil { |
| 40 | + return database.Endpoint{}, err |
| 41 | + } |
| 42 | + |
| 43 | + if name != "" { |
| 44 | + existingEndpoint.Name = name |
| 45 | + } |
| 46 | + if method != "" { |
| 47 | + existingEndpoint.Method = method |
| 48 | + } |
| 49 | + if url != "" { |
| 50 | + existingEndpoint.Url = url |
| 51 | + } |
| 52 | + if headers != "" { |
| 53 | + if !json.Valid([]byte(headers)) { |
| 54 | + return database.Endpoint{}, fmt.Errorf("invalid JSON for header parameters: %s", headers) |
| 55 | + } |
| 56 | + existingEndpoint.Headers = headers |
| 57 | + } |
| 58 | + if queryParams != "" { |
| 59 | + if !json.Valid([]byte(queryParams)) { |
| 60 | + return database.Endpoint{}, fmt.Errorf("invalid JSON for query parameters: %s", queryParams) |
| 61 | + } |
| 62 | + existingEndpoint.QueryParams = queryParams |
| 63 | + } |
| 64 | + if requestBody != "" { |
| 65 | + existingEndpoint.RequestBody = requestBody |
| 66 | + } |
| 67 | + |
| 68 | + return c.DB.UpdateEndpoint(ctx, database.UpdateEndpointParams{ |
| 69 | + ID: existingEndpoint.ID, |
| 70 | + Name: existingEndpoint.Name, |
| 71 | + Method: existingEndpoint.Method, |
| 72 | + Url: existingEndpoint.Url, |
| 73 | + Headers: existingEndpoint.Headers, |
| 74 | + QueryParams: existingEndpoint.QueryParams, |
| 75 | + RequestBody: existingEndpoint.RequestBody, |
| 76 | + }) |
| 77 | +} |
0 commit comments