From 16fc2aaa294310a2d147634aecf343e072f8faa0 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 11 Sep 2022 16:11:04 -0800 Subject: [PATCH] attachment type updates --- backend/app/api/docs/docs.go | 627 ++---------------- backend/app/api/docs/swagger.json | 627 ++---------------- backend/app/api/docs/swagger.yaml | 466 ++----------- backend/app/api/v1/v1_ctrl_user.go | 2 +- backend/internal/services/all.go | 5 +- backend/internal/services/main_test.go | 2 + backend/internal/services/mappers/items.go | 19 + backend/internal/services/service_items.go | 59 ++ .../internal/services/service_items_test.go | 66 +- backend/internal/types/document_types.go | 1 + 10 files changed, 311 insertions(+), 1563 deletions(-) diff --git a/backend/app/api/docs/docs.go b/backend/app/api/docs/docs.go index 7c479c2..4e49256 100644 --- a/backend/app/api/docs/docs.go +++ b/backend/app/api/docs/docs.go @@ -691,7 +691,7 @@ const docTemplate = `{ "type": "object", "properties": { "item": { - "$ref": "#/definitions/ent.User" + "$ref": "#/definitions/types.UserOut" } } } @@ -788,571 +788,6 @@ const docTemplate = `{ } }, "definitions": { - "ent.Attachment": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the AttachmentQuery when eager-loading is set.", - "$ref": "#/definitions/ent.AttachmentEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "type": { - "description": "Type holds the value of the \"type\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.AttachmentEdges": { - "type": "object", - "properties": { - "document": { - "description": "Document holds the value of the document edge.", - "$ref": "#/definitions/ent.Document" - }, - "item": { - "description": "Item holds the value of the item edge.", - "$ref": "#/definitions/ent.Item" - } - } - }, - "ent.AuthTokens": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the AuthTokensQuery when eager-loading is set.", - "$ref": "#/definitions/ent.AuthTokensEdges" - }, - "expires_at": { - "description": "ExpiresAt holds the value of the \"expires_at\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "token": { - "description": "Token holds the value of the \"token\" field.", - "type": "array", - "items": { - "type": "integer" - } - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.AuthTokensEdges": { - "type": "object", - "properties": { - "user": { - "description": "User holds the value of the user edge.", - "$ref": "#/definitions/ent.User" - } - } - }, - "ent.Document": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the DocumentQuery when eager-loading is set.", - "$ref": "#/definitions/ent.DocumentEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "path": { - "description": "Path holds the value of the \"path\" field.", - "type": "string" - }, - "title": { - "description": "Title holds the value of the \"title\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.DocumentEdges": { - "type": "object", - "properties": { - "attachments": { - "description": "Attachments holds the value of the attachments edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Attachment" - } - }, - "document_tokens": { - "description": "DocumentTokens holds the value of the document_tokens edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.DocumentToken" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - } - } - }, - "ent.DocumentToken": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the DocumentTokenQuery when eager-loading is set.", - "$ref": "#/definitions/ent.DocumentTokenEdges" - }, - "expires_at": { - "description": "ExpiresAt holds the value of the \"expires_at\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "token": { - "description": "Token holds the value of the \"token\" field.", - "type": "array", - "items": { - "type": "integer" - } - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - }, - "uses": { - "description": "Uses holds the value of the \"uses\" field.", - "type": "integer" - } - } - }, - "ent.DocumentTokenEdges": { - "type": "object", - "properties": { - "document": { - "description": "Document holds the value of the document edge.", - "$ref": "#/definitions/ent.Document" - } - } - }, - "ent.Group": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "currency": { - "description": "Currency holds the value of the \"currency\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the GroupQuery when eager-loading is set.", - "$ref": "#/definitions/ent.GroupEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.GroupEdges": { - "type": "object", - "properties": { - "documents": { - "description": "Documents holds the value of the documents edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Document" - } - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - }, - "labels": { - "description": "Labels holds the value of the labels edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Label" - } - }, - "locations": { - "description": "Locations holds the value of the locations edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Location" - } - }, - "users": { - "description": "Users holds the value of the users edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.User" - } - } - } - }, - "ent.Item": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the ItemQuery when eager-loading is set.", - "$ref": "#/definitions/ent.ItemEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "lifetime_warranty": { - "description": "LifetimeWarranty holds the value of the \"lifetime_warranty\" field.", - "type": "boolean" - }, - "manufacturer": { - "description": "Manufacturer holds the value of the \"manufacturer\" field.", - "type": "string" - }, - "model_number": { - "description": "ModelNumber holds the value of the \"model_number\" field.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "notes": { - "description": "Notes holds the value of the \"notes\" field.", - "type": "string" - }, - "purchase_from": { - "description": "PurchaseFrom holds the value of the \"purchase_from\" field.", - "type": "string" - }, - "purchase_price": { - "description": "PurchasePrice holds the value of the \"purchase_price\" field.", - "type": "number" - }, - "purchase_time": { - "description": "PurchaseTime holds the value of the \"purchase_time\" field.", - "type": "string" - }, - "serial_number": { - "description": "SerialNumber holds the value of the \"serial_number\" field.", - "type": "string" - }, - "sold_notes": { - "description": "SoldNotes holds the value of the \"sold_notes\" field.", - "type": "string" - }, - "sold_price": { - "description": "SoldPrice holds the value of the \"sold_price\" field.", - "type": "number" - }, - "sold_time": { - "description": "SoldTime holds the value of the \"sold_time\" field.", - "type": "string" - }, - "sold_to": { - "description": "SoldTo holds the value of the \"sold_to\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - }, - "warranty_details": { - "description": "WarrantyDetails holds the value of the \"warranty_details\" field.", - "type": "string" - }, - "warranty_expires": { - "description": "WarrantyExpires holds the value of the \"warranty_expires\" field.", - "type": "string" - } - } - }, - "ent.ItemEdges": { - "type": "object", - "properties": { - "attachments": { - "description": "Attachments holds the value of the attachments edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Attachment" - } - }, - "fields": { - "description": "Fields holds the value of the fields edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.ItemField" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "label": { - "description": "Label holds the value of the label edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Label" - } - }, - "location": { - "description": "Location holds the value of the location edge.", - "$ref": "#/definitions/ent.Location" - } - } - }, - "ent.ItemField": { - "type": "object", - "properties": { - "boolean_value": { - "description": "BooleanValue holds the value of the \"boolean_value\" field.", - "type": "boolean" - }, - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the ItemFieldQuery when eager-loading is set.", - "$ref": "#/definitions/ent.ItemFieldEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "number_value": { - "description": "NumberValue holds the value of the \"number_value\" field.", - "type": "integer" - }, - "text_value": { - "description": "TextValue holds the value of the \"text_value\" field.", - "type": "string" - }, - "time_value": { - "description": "TimeValue holds the value of the \"time_value\" field.", - "type": "string" - }, - "type": { - "description": "Type holds the value of the \"type\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.ItemFieldEdges": { - "type": "object", - "properties": { - "item": { - "description": "Item holds the value of the item edge.", - "$ref": "#/definitions/ent.Item" - } - } - }, - "ent.Label": { - "type": "object", - "properties": { - "color": { - "description": "Color holds the value of the \"color\" field.", - "type": "string" - }, - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the LabelQuery when eager-loading is set.", - "$ref": "#/definitions/ent.LabelEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.LabelEdges": { - "type": "object", - "properties": { - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - } - } - }, - "ent.Location": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the LocationQuery when eager-loading is set.", - "$ref": "#/definitions/ent.LocationEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.LocationEdges": { - "type": "object", - "properties": { - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - } - } - }, - "ent.User": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the UserQuery when eager-loading is set.", - "$ref": "#/definitions/ent.UserEdges" - }, - "email": { - "description": "Email holds the value of the \"email\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "is_superuser": { - "description": "IsSuperuser holds the value of the \"is_superuser\" field.", - "type": "boolean" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.UserEdges": { - "type": "object", - "properties": { - "auth_tokens": { - "description": "AuthTokens holds the value of the auth_tokens edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.AuthTokens" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - } - } - }, "server.Result": { "type": "object", "properties": { @@ -1394,6 +829,37 @@ const docTemplate = `{ } } }, + "types.DocumentOut": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "path": { + "type": "string" + }, + "title": { + "type": "string" + } + } + }, + "types.ItemAttachment": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "document": { + "$ref": "#/definitions/types.DocumentOut" + }, + "id": { + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, "types.ItemCreate": { "type": "object", "properties": { @@ -1418,6 +884,12 @@ const docTemplate = `{ "types.ItemOut": { "type": "object", "properties": { + "attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/types.ItemAttachment" + } + }, "createdAt": { "type": "string" }, @@ -1740,6 +1212,29 @@ const docTemplate = `{ } } }, + "types.UserOut": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "groupId": { + "type": "string" + }, + "groupName": { + "type": "string" + }, + "id": { + "type": "string" + }, + "isSuperuser": { + "type": "boolean" + }, + "name": { + "type": "string" + } + } + }, "types.UserRegistration": { "type": "object", "properties": { diff --git a/backend/app/api/docs/swagger.json b/backend/app/api/docs/swagger.json index be5f31d..f2a7dbc 100644 --- a/backend/app/api/docs/swagger.json +++ b/backend/app/api/docs/swagger.json @@ -683,7 +683,7 @@ "type": "object", "properties": { "item": { - "$ref": "#/definitions/ent.User" + "$ref": "#/definitions/types.UserOut" } } } @@ -780,571 +780,6 @@ } }, "definitions": { - "ent.Attachment": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the AttachmentQuery when eager-loading is set.", - "$ref": "#/definitions/ent.AttachmentEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "type": { - "description": "Type holds the value of the \"type\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.AttachmentEdges": { - "type": "object", - "properties": { - "document": { - "description": "Document holds the value of the document edge.", - "$ref": "#/definitions/ent.Document" - }, - "item": { - "description": "Item holds the value of the item edge.", - "$ref": "#/definitions/ent.Item" - } - } - }, - "ent.AuthTokens": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the AuthTokensQuery when eager-loading is set.", - "$ref": "#/definitions/ent.AuthTokensEdges" - }, - "expires_at": { - "description": "ExpiresAt holds the value of the \"expires_at\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "token": { - "description": "Token holds the value of the \"token\" field.", - "type": "array", - "items": { - "type": "integer" - } - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.AuthTokensEdges": { - "type": "object", - "properties": { - "user": { - "description": "User holds the value of the user edge.", - "$ref": "#/definitions/ent.User" - } - } - }, - "ent.Document": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the DocumentQuery when eager-loading is set.", - "$ref": "#/definitions/ent.DocumentEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "path": { - "description": "Path holds the value of the \"path\" field.", - "type": "string" - }, - "title": { - "description": "Title holds the value of the \"title\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.DocumentEdges": { - "type": "object", - "properties": { - "attachments": { - "description": "Attachments holds the value of the attachments edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Attachment" - } - }, - "document_tokens": { - "description": "DocumentTokens holds the value of the document_tokens edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.DocumentToken" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - } - } - }, - "ent.DocumentToken": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the DocumentTokenQuery when eager-loading is set.", - "$ref": "#/definitions/ent.DocumentTokenEdges" - }, - "expires_at": { - "description": "ExpiresAt holds the value of the \"expires_at\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "token": { - "description": "Token holds the value of the \"token\" field.", - "type": "array", - "items": { - "type": "integer" - } - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - }, - "uses": { - "description": "Uses holds the value of the \"uses\" field.", - "type": "integer" - } - } - }, - "ent.DocumentTokenEdges": { - "type": "object", - "properties": { - "document": { - "description": "Document holds the value of the document edge.", - "$ref": "#/definitions/ent.Document" - } - } - }, - "ent.Group": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "currency": { - "description": "Currency holds the value of the \"currency\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the GroupQuery when eager-loading is set.", - "$ref": "#/definitions/ent.GroupEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.GroupEdges": { - "type": "object", - "properties": { - "documents": { - "description": "Documents holds the value of the documents edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Document" - } - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - }, - "labels": { - "description": "Labels holds the value of the labels edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Label" - } - }, - "locations": { - "description": "Locations holds the value of the locations edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Location" - } - }, - "users": { - "description": "Users holds the value of the users edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.User" - } - } - } - }, - "ent.Item": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the ItemQuery when eager-loading is set.", - "$ref": "#/definitions/ent.ItemEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "lifetime_warranty": { - "description": "LifetimeWarranty holds the value of the \"lifetime_warranty\" field.", - "type": "boolean" - }, - "manufacturer": { - "description": "Manufacturer holds the value of the \"manufacturer\" field.", - "type": "string" - }, - "model_number": { - "description": "ModelNumber holds the value of the \"model_number\" field.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "notes": { - "description": "Notes holds the value of the \"notes\" field.", - "type": "string" - }, - "purchase_from": { - "description": "PurchaseFrom holds the value of the \"purchase_from\" field.", - "type": "string" - }, - "purchase_price": { - "description": "PurchasePrice holds the value of the \"purchase_price\" field.", - "type": "number" - }, - "purchase_time": { - "description": "PurchaseTime holds the value of the \"purchase_time\" field.", - "type": "string" - }, - "serial_number": { - "description": "SerialNumber holds the value of the \"serial_number\" field.", - "type": "string" - }, - "sold_notes": { - "description": "SoldNotes holds the value of the \"sold_notes\" field.", - "type": "string" - }, - "sold_price": { - "description": "SoldPrice holds the value of the \"sold_price\" field.", - "type": "number" - }, - "sold_time": { - "description": "SoldTime holds the value of the \"sold_time\" field.", - "type": "string" - }, - "sold_to": { - "description": "SoldTo holds the value of the \"sold_to\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - }, - "warranty_details": { - "description": "WarrantyDetails holds the value of the \"warranty_details\" field.", - "type": "string" - }, - "warranty_expires": { - "description": "WarrantyExpires holds the value of the \"warranty_expires\" field.", - "type": "string" - } - } - }, - "ent.ItemEdges": { - "type": "object", - "properties": { - "attachments": { - "description": "Attachments holds the value of the attachments edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Attachment" - } - }, - "fields": { - "description": "Fields holds the value of the fields edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.ItemField" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "label": { - "description": "Label holds the value of the label edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Label" - } - }, - "location": { - "description": "Location holds the value of the location edge.", - "$ref": "#/definitions/ent.Location" - } - } - }, - "ent.ItemField": { - "type": "object", - "properties": { - "boolean_value": { - "description": "BooleanValue holds the value of the \"boolean_value\" field.", - "type": "boolean" - }, - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the ItemFieldQuery when eager-loading is set.", - "$ref": "#/definitions/ent.ItemFieldEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "number_value": { - "description": "NumberValue holds the value of the \"number_value\" field.", - "type": "integer" - }, - "text_value": { - "description": "TextValue holds the value of the \"text_value\" field.", - "type": "string" - }, - "time_value": { - "description": "TimeValue holds the value of the \"time_value\" field.", - "type": "string" - }, - "type": { - "description": "Type holds the value of the \"type\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.ItemFieldEdges": { - "type": "object", - "properties": { - "item": { - "description": "Item holds the value of the item edge.", - "$ref": "#/definitions/ent.Item" - } - } - }, - "ent.Label": { - "type": "object", - "properties": { - "color": { - "description": "Color holds the value of the \"color\" field.", - "type": "string" - }, - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the LabelQuery when eager-loading is set.", - "$ref": "#/definitions/ent.LabelEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.LabelEdges": { - "type": "object", - "properties": { - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - } - } - }, - "ent.Location": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "description": { - "description": "Description holds the value of the \"description\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the LocationQuery when eager-loading is set.", - "$ref": "#/definitions/ent.LocationEdges" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.LocationEdges": { - "type": "object", - "properties": { - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - }, - "items": { - "description": "Items holds the value of the items edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.Item" - } - } - } - }, - "ent.User": { - "type": "object", - "properties": { - "created_at": { - "description": "CreatedAt holds the value of the \"created_at\" field.", - "type": "string" - }, - "edges": { - "description": "Edges holds the relations/edges for other nodes in the graph.\nThe values are being populated by the UserQuery when eager-loading is set.", - "$ref": "#/definitions/ent.UserEdges" - }, - "email": { - "description": "Email holds the value of the \"email\" field.", - "type": "string" - }, - "id": { - "description": "ID of the ent.", - "type": "string" - }, - "is_superuser": { - "description": "IsSuperuser holds the value of the \"is_superuser\" field.", - "type": "boolean" - }, - "name": { - "description": "Name holds the value of the \"name\" field.", - "type": "string" - }, - "updated_at": { - "description": "UpdatedAt holds the value of the \"updated_at\" field.", - "type": "string" - } - } - }, - "ent.UserEdges": { - "type": "object", - "properties": { - "auth_tokens": { - "description": "AuthTokens holds the value of the auth_tokens edge.", - "type": "array", - "items": { - "$ref": "#/definitions/ent.AuthTokens" - } - }, - "group": { - "description": "Group holds the value of the group edge.", - "$ref": "#/definitions/ent.Group" - } - } - }, "server.Result": { "type": "object", "properties": { @@ -1386,6 +821,37 @@ } } }, + "types.DocumentOut": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "path": { + "type": "string" + }, + "title": { + "type": "string" + } + } + }, + "types.ItemAttachment": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "document": { + "$ref": "#/definitions/types.DocumentOut" + }, + "id": { + "type": "string" + }, + "updatedAt": { + "type": "string" + } + } + }, "types.ItemCreate": { "type": "object", "properties": { @@ -1410,6 +876,12 @@ "types.ItemOut": { "type": "object", "properties": { + "attachments": { + "type": "array", + "items": { + "$ref": "#/definitions/types.ItemAttachment" + } + }, "createdAt": { "type": "string" }, @@ -1732,6 +1204,29 @@ } } }, + "types.UserOut": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "groupId": { + "type": "string" + }, + "groupName": { + "type": "string" + }, + "id": { + "type": "string" + }, + "isSuperuser": { + "type": "boolean" + }, + "name": { + "type": "string" + } + } + }, "types.UserRegistration": { "type": "object", "properties": { diff --git a/backend/app/api/docs/swagger.yaml b/backend/app/api/docs/swagger.yaml index 57a0218..eee6e0b 100644 --- a/backend/app/api/docs/swagger.yaml +++ b/backend/app/api/docs/swagger.yaml @@ -1,430 +1,5 @@ basePath: /api definitions: - ent.Attachment: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - edges: - $ref: '#/definitions/ent.AttachmentEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the AttachmentQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - type: - description: Type holds the value of the "type" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.AttachmentEdges: - properties: - document: - $ref: '#/definitions/ent.Document' - description: Document holds the value of the document edge. - item: - $ref: '#/definitions/ent.Item' - description: Item holds the value of the item edge. - type: object - ent.AuthTokens: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - edges: - $ref: '#/definitions/ent.AuthTokensEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the AuthTokensQuery when eager-loading is set. - expires_at: - description: ExpiresAt holds the value of the "expires_at" field. - type: string - id: - description: ID of the ent. - type: string - token: - description: Token holds the value of the "token" field. - items: - type: integer - type: array - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.AuthTokensEdges: - properties: - user: - $ref: '#/definitions/ent.User' - description: User holds the value of the user edge. - type: object - ent.Document: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - edges: - $ref: '#/definitions/ent.DocumentEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the DocumentQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - path: - description: Path holds the value of the "path" field. - type: string - title: - description: Title holds the value of the "title" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.DocumentEdges: - properties: - attachments: - description: Attachments holds the value of the attachments edge. - items: - $ref: '#/definitions/ent.Attachment' - type: array - document_tokens: - description: DocumentTokens holds the value of the document_tokens edge. - items: - $ref: '#/definitions/ent.DocumentToken' - type: array - group: - $ref: '#/definitions/ent.Group' - description: Group holds the value of the group edge. - type: object - ent.DocumentToken: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - edges: - $ref: '#/definitions/ent.DocumentTokenEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the DocumentTokenQuery when eager-loading is set. - expires_at: - description: ExpiresAt holds the value of the "expires_at" field. - type: string - id: - description: ID of the ent. - type: string - token: - description: Token holds the value of the "token" field. - items: - type: integer - type: array - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - uses: - description: Uses holds the value of the "uses" field. - type: integer - type: object - ent.DocumentTokenEdges: - properties: - document: - $ref: '#/definitions/ent.Document' - description: Document holds the value of the document edge. - type: object - ent.Group: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - currency: - description: Currency holds the value of the "currency" field. - type: string - edges: - $ref: '#/definitions/ent.GroupEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the GroupQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - name: - description: Name holds the value of the "name" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.GroupEdges: - properties: - documents: - description: Documents holds the value of the documents edge. - items: - $ref: '#/definitions/ent.Document' - type: array - items: - description: Items holds the value of the items edge. - items: - $ref: '#/definitions/ent.Item' - type: array - labels: - description: Labels holds the value of the labels edge. - items: - $ref: '#/definitions/ent.Label' - type: array - locations: - description: Locations holds the value of the locations edge. - items: - $ref: '#/definitions/ent.Location' - type: array - users: - description: Users holds the value of the users edge. - items: - $ref: '#/definitions/ent.User' - type: array - type: object - ent.Item: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - description: - description: Description holds the value of the "description" field. - type: string - edges: - $ref: '#/definitions/ent.ItemEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the ItemQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - lifetime_warranty: - description: LifetimeWarranty holds the value of the "lifetime_warranty" field. - type: boolean - manufacturer: - description: Manufacturer holds the value of the "manufacturer" field. - type: string - model_number: - description: ModelNumber holds the value of the "model_number" field. - type: string - name: - description: Name holds the value of the "name" field. - type: string - notes: - description: Notes holds the value of the "notes" field. - type: string - purchase_from: - description: PurchaseFrom holds the value of the "purchase_from" field. - type: string - purchase_price: - description: PurchasePrice holds the value of the "purchase_price" field. - type: number - purchase_time: - description: PurchaseTime holds the value of the "purchase_time" field. - type: string - serial_number: - description: SerialNumber holds the value of the "serial_number" field. - type: string - sold_notes: - description: SoldNotes holds the value of the "sold_notes" field. - type: string - sold_price: - description: SoldPrice holds the value of the "sold_price" field. - type: number - sold_time: - description: SoldTime holds the value of the "sold_time" field. - type: string - sold_to: - description: SoldTo holds the value of the "sold_to" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - warranty_details: - description: WarrantyDetails holds the value of the "warranty_details" field. - type: string - warranty_expires: - description: WarrantyExpires holds the value of the "warranty_expires" field. - type: string - type: object - ent.ItemEdges: - properties: - attachments: - description: Attachments holds the value of the attachments edge. - items: - $ref: '#/definitions/ent.Attachment' - type: array - fields: - description: Fields holds the value of the fields edge. - items: - $ref: '#/definitions/ent.ItemField' - type: array - group: - $ref: '#/definitions/ent.Group' - description: Group holds the value of the group edge. - label: - description: Label holds the value of the label edge. - items: - $ref: '#/definitions/ent.Label' - type: array - location: - $ref: '#/definitions/ent.Location' - description: Location holds the value of the location edge. - type: object - ent.ItemField: - properties: - boolean_value: - description: BooleanValue holds the value of the "boolean_value" field. - type: boolean - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - description: - description: Description holds the value of the "description" field. - type: string - edges: - $ref: '#/definitions/ent.ItemFieldEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the ItemFieldQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - name: - description: Name holds the value of the "name" field. - type: string - number_value: - description: NumberValue holds the value of the "number_value" field. - type: integer - text_value: - description: TextValue holds the value of the "text_value" field. - type: string - time_value: - description: TimeValue holds the value of the "time_value" field. - type: string - type: - description: Type holds the value of the "type" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.ItemFieldEdges: - properties: - item: - $ref: '#/definitions/ent.Item' - description: Item holds the value of the item edge. - type: object - ent.Label: - properties: - color: - description: Color holds the value of the "color" field. - type: string - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - description: - description: Description holds the value of the "description" field. - type: string - edges: - $ref: '#/definitions/ent.LabelEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the LabelQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - name: - description: Name holds the value of the "name" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.LabelEdges: - properties: - group: - $ref: '#/definitions/ent.Group' - description: Group holds the value of the group edge. - items: - description: Items holds the value of the items edge. - items: - $ref: '#/definitions/ent.Item' - type: array - type: object - ent.Location: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - description: - description: Description holds the value of the "description" field. - type: string - edges: - $ref: '#/definitions/ent.LocationEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the LocationQuery when eager-loading is set. - id: - description: ID of the ent. - type: string - name: - description: Name holds the value of the "name" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.LocationEdges: - properties: - group: - $ref: '#/definitions/ent.Group' - description: Group holds the value of the group edge. - items: - description: Items holds the value of the items edge. - items: - $ref: '#/definitions/ent.Item' - type: array - type: object - ent.User: - properties: - created_at: - description: CreatedAt holds the value of the "created_at" field. - type: string - edges: - $ref: '#/definitions/ent.UserEdges' - description: |- - Edges holds the relations/edges for other nodes in the graph. - The values are being populated by the UserQuery when eager-loading is set. - email: - description: Email holds the value of the "email" field. - type: string - id: - description: ID of the ent. - type: string - is_superuser: - description: IsSuperuser holds the value of the "is_superuser" field. - type: boolean - name: - description: Name holds the value of the "name" field. - type: string - updated_at: - description: UpdatedAt holds the value of the "updated_at" field. - type: string - type: object - ent.UserEdges: - properties: - auth_tokens: - description: AuthTokens holds the value of the auth_tokens edge. - items: - $ref: '#/definitions/ent.AuthTokens' - type: array - group: - $ref: '#/definitions/ent.Group' - description: Group holds the value of the group edge. - type: object server.Result: properties: details: {} @@ -452,6 +27,26 @@ definitions: type: string type: array type: object + types.DocumentOut: + properties: + id: + type: string + path: + type: string + title: + type: string + type: object + types.ItemAttachment: + properties: + createdAt: + type: string + document: + $ref: '#/definitions/types.DocumentOut' + id: + type: string + updatedAt: + type: string + type: object types.ItemCreate: properties: description: @@ -468,6 +63,10 @@ definitions: type: object types.ItemOut: properties: + attachments: + items: + $ref: '#/definitions/types.ItemAttachment' + type: array createdAt: type: string description: @@ -683,6 +282,21 @@ definitions: password: type: string type: object + types.UserOut: + properties: + email: + type: string + groupId: + type: string + groupName: + type: string + id: + type: string + isSuperuser: + type: boolean + name: + type: string + type: object types.UserRegistration: properties: groupName: @@ -1118,7 +732,7 @@ paths: - $ref: '#/definitions/server.Result' - properties: item: - $ref: '#/definitions/ent.User' + $ref: '#/definitions/types.UserOut' type: object security: - Bearer: [] diff --git a/backend/app/api/v1/v1_ctrl_user.go b/backend/app/api/v1/v1_ctrl_user.go index 520fd81..f7c4de6 100644 --- a/backend/app/api/v1/v1_ctrl_user.go +++ b/backend/app/api/v1/v1_ctrl_user.go @@ -41,7 +41,7 @@ func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc { // @Summary Get the current user // @Tags User // @Produce json -// @Success 200 {object} server.Result{item=ent.User} +// @Success 200 {object} server.Result{item=types.UserOut} // @Router /v1/users/self [GET] // @Security Bearer func (ctrl *V1Controller) HandleUserSelf() http.HandlerFunc { diff --git a/backend/internal/services/all.go b/backend/internal/services/all.go index 8978983..bba6e43 100644 --- a/backend/internal/services/all.go +++ b/backend/internal/services/all.go @@ -16,6 +16,9 @@ func NewServices(repos *repo.AllRepos) *AllServices { Admin: &AdminService{repos}, Location: &LocationService{repos}, Labels: &LabelService{repos}, - Items: &ItemService{repos}, + Items: &ItemService{ + repo: repos, + filepath: "/tmp/content", + }, } } diff --git a/backend/internal/services/main_test.go b/backend/internal/services/main_test.go index 0f5e78a..c5f3575 100644 --- a/backend/internal/services/main_test.go +++ b/backend/internal/services/main_test.go @@ -22,6 +22,7 @@ var ( tRepos *repo.AllRepos tUser *ent.User tGroup *ent.Group + tSvc *AllServices ) func bootstrap() { @@ -62,6 +63,7 @@ func TestMain(m *testing.M) { tClient = client tRepos = repo.EntAllRepos(tClient) + tSvc = NewServices(tRepos) defer client.Close() bootstrap() diff --git a/backend/internal/services/mappers/items.go b/backend/internal/services/mappers/items.go index 11ec73b..ed29e3e 100644 --- a/backend/internal/services/mappers/items.go +++ b/backend/internal/services/mappers/items.go @@ -5,6 +5,19 @@ import ( "github.com/hay-kot/content/backend/internal/types" ) +func ToItemAttachment(attachment *ent.Attachment) *types.ItemAttachment { + return &types.ItemAttachment{ + ID: attachment.ID, + CreatedAt: attachment.CreatedAt, + UpdatedAt: attachment.UpdatedAt, + Document: types.DocumentOut{ + ID: attachment.Edges.Document.ID, + Title: attachment.Edges.Document.Title, + Path: attachment.Edges.Document.Path, + }, + } +} + func ToItemSummary(item *ent.Item) *types.ItemSummary { var location *types.LocationSummary if item.Edges.Location != nil { @@ -53,8 +66,14 @@ func ToItemSummaryErr(item *ent.Item, err error) (*types.ItemSummary, error) { } func ToItemOut(item *ent.Item) *types.ItemOut { + var attachments []*types.ItemAttachment + if item.Edges.Attachments != nil { + attachments = MapEach(item.Edges.Attachments, ToItemAttachment) + } + return &types.ItemOut{ ItemSummary: *ToItemSummary(item), + Attachments: attachments, } } diff --git a/backend/internal/services/service_items.go b/backend/internal/services/service_items.go index 40ac207..513c838 100644 --- a/backend/internal/services/service_items.go +++ b/backend/internal/services/service_items.go @@ -3,8 +3,12 @@ package services import ( "context" "fmt" + "io" + "os" + "path/filepath" "github.com/google/uuid" + "github.com/hay-kot/content/backend/ent/attachment" "github.com/hay-kot/content/backend/internal/repo" "github.com/hay-kot/content/backend/internal/services/mappers" "github.com/hay-kot/content/backend/internal/types" @@ -13,6 +17,9 @@ import ( type ItemService struct { repo *repo.AllRepos + + // filepath is the root of the storage location that will be used to store all files from. + filepath string } func (svc *ItemService) GetOne(ctx context.Context, gid uuid.UUID, id uuid.UUID) (*types.ItemOut, error) { @@ -41,6 +48,7 @@ func (svc *ItemService) GetAll(ctx context.Context, gid uuid.UUID) ([]*types.Ite return itemsOut, nil } + func (svc *ItemService) Create(ctx context.Context, gid uuid.UUID, data types.ItemCreate) (*types.ItemOut, error) { item, err := svc.repo.Items.Create(ctx, gid, data) if err != nil { @@ -49,6 +57,7 @@ func (svc *ItemService) Create(ctx context.Context, gid uuid.UUID, data types.It return mappers.ToItemOut(item), nil } + func (svc *ItemService) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) error { item, err := svc.repo.Items.GetOne(ctx, id) if err != nil { @@ -66,10 +75,60 @@ func (svc *ItemService) Delete(ctx context.Context, gid uuid.UUID, id uuid.UUID) return nil } + func (svc *ItemService) Update(ctx context.Context, gid uuid.UUID, data types.ItemUpdate) (*types.ItemOut, error) { panic("implement me") } +func (svc *ItemService) attachmentPath(gid, itemId uuid.UUID, filename string) string { + return filepath.Join(svc.filepath, gid.String(), itemId.String(), filename) +} + +// AddAttachment adds an attachment to an item by creating an entry in the Documents table and linking it to the Attachment +// Table and Items table. The file provided via the reader is stored on the file system based on the provided +// relative path during construction of the service. +func (svc *ItemService) AddAttachment(ctx context.Context, gid, itemId uuid.UUID, filename string, file io.Reader) (*types.ItemOut, error) { + // Get the Item + item, err := svc.repo.Items.GetOne(ctx, itemId) + if err != nil { + return nil, err + } + + if item.Edges.Group.ID != gid { + return nil, ErrNotOwner + } + + // Create the document + doc, err := svc.repo.Docs.Create(ctx, gid, types.DocumentCreate{ + Title: filename, + Path: svc.attachmentPath(gid, itemId, filename), + }) + if err != nil { + return nil, err + } + + // Create the attachment + _, err = svc.repo.Attachments.Create(ctx, itemId, doc.ID, attachment.TypeAttachment) + if err != nil { + return nil, err + } + + // Read the contents and write them to a file on the file system + os.MkdirAll(filepath.Dir(doc.Path), os.ModePerm) + f, err := os.Create(doc.Path) + if err != nil { + log.Err(err).Msg("failed to create file") + return nil, err + } + + _, err = io.Copy(f, file) + if err != nil { + return nil, err + } + + return svc.GetOne(ctx, gid, itemId) +} + func (svc *ItemService) CsvImport(ctx context.Context, gid uuid.UUID, data [][]string) error { loaded := []csvRow{} diff --git a/backend/internal/services/service_items_test.go b/backend/internal/services/service_items_test.go index d5c18c0..ceeb764 100644 --- a/backend/internal/services/service_items_test.go +++ b/backend/internal/services/service_items_test.go @@ -2,13 +2,16 @@ package services import ( "context" + "os" + "path" + "strings" "testing" + "github.com/google/uuid" + "github.com/hay-kot/content/backend/internal/types" "github.com/stretchr/testify/assert" ) - - func TestItemService_CsvImport(t *testing.T) { data := loadcsv() svc := &ItemService{ @@ -55,6 +58,14 @@ func TestItemService_CsvImport(t *testing.T) { labelNames = append(labelNames, label.Name) } + ids := []uuid.UUID{} + t.Cleanup((func() { + for _, id := range ids { + err := svc.repo.Items.Delete(context.Background(), id) + assert.NoError(t, err) + } + })) + for _, item := range items { assert.Contains(t, locNames, item.Location.Name) for _, label := range item.Labels { @@ -79,6 +90,55 @@ func TestItemService_CsvImport(t *testing.T) { assert.Equal(t, csvRow.parsedSoldPrice(), item.SoldPrice) } } - } } + +func TestItemService_AddAttachment(t *testing.T) { + temp := os.TempDir() + + svc := &ItemService{ + repo: tRepos, + filepath: temp, + } + + loc, err := tSvc.Location.Create(context.Background(), tGroup.ID, types.LocationCreate{ + Description: "test", + Name: "test", + }) + assert.NoError(t, err) + assert.NotNil(t, loc) + + itmC := types.ItemCreate{ + Name: fk.Str(10), + Description: fk.Str(10), + LocationID: loc.ID, + } + + itm, err := svc.Create(context.Background(), tGroup.ID, itmC) + assert.NoError(t, err) + assert.NotNil(t, itm) + t.Cleanup(func() { + err := svc.repo.Items.Delete(context.Background(), itm.ID) + assert.NoError(t, err) + }) + + contents := fk.Str(1000) + reader := strings.NewReader(contents) + + // Setup + afterAttachment, err := svc.AddAttachment(context.Background(), tGroup.ID, itm.ID, "testfile.txt", reader) + assert.NoError(t, err) + assert.NotNil(t, afterAttachment) + + // Check that the file exists + storedPath := afterAttachment.Attachments[0].Document.Path + + // {root}/{group}/{item}/{attachment} + assert.Equal(t, path.Join(temp, tGroup.ID.String(), itm.ID.String(), "testfile.txt"), storedPath) + + // Check that the file contents are correct + bts, err := os.ReadFile(storedPath) + assert.NoError(t, err) + assert.Equal(t, contents, string(bts)) + +} diff --git a/backend/internal/types/document_types.go b/backend/internal/types/document_types.go index f867504..c34a37a 100644 --- a/backend/internal/types/document_types.go +++ b/backend/internal/types/document_types.go @@ -9,6 +9,7 @@ import ( type DocumentOut struct { ID uuid.UUID `json:"id"` Title string `json:"title"` + Path string } type DocumentCreate struct {