From abf995221226519667c534d9f9228823303fa426 Mon Sep 17 00:00:00 2001 From: Twilight Date: Sun, 21 Jul 2024 01:20:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/rag/document.pb.go | 24 ++-- api/rag/document.proto | 19 ++- api/rag/rag.swagger.json | 9 +- api/rag/rag_grpc.pb.go | 4 +- ent/document.go | 13 +- ent/document/document.go | 8 ++ ent/document/where.go | 45 ++++++ ent/document_create.go | 13 ++ ent/document_update.go | 54 ++++++++ ent/documentblock.go | 8 +- ent/documentblock/where.go | 36 ++--- ent/documentblock_create.go | 8 +- ent/documentblock_update.go | 40 +++--- ent/migrate/schema.go | 7 +- ent/mutation.go | 130 +++++++++++++++--- ent/runtime.go | 4 +- ent/schema/document.go | 1 + ent/schema/documentblock.go | 4 +- .../handlers/controllers/library/library.go | 20 +-- internal/logic/document.go | 40 +++++- internal/logic/library.go | 32 ++++- .../20240714155720_create_library.sql | 11 -- .../20240714164619_update_user_id.sql | 7 - .../20240720092815_add_documents_tables.sql | 19 --- ...720093157_add_order_to_document_blocks.sql | 11 -- internal/migrations/20240720155522_init.sql | 27 ++++ internal/migrations/atlas.sum | 7 +- internal/routes/router.go | 2 +- internal/services/rag/documentService.go | 5 +- internal/services/rag/libraryservice.go | 17 ++- 30 files changed, 452 insertions(+), 173 deletions(-) delete mode 100644 internal/migrations/20240714155720_create_library.sql delete mode 100644 internal/migrations/20240714164619_update_user_id.sql delete mode 100644 internal/migrations/20240720092815_add_documents_tables.sql delete mode 100644 internal/migrations/20240720093157_add_order_to_document_blocks.sql create mode 100644 internal/migrations/20240720155522_init.sql diff --git a/api/rag/document.pb.go b/api/rag/document.pb.go index 40613f1..92d2dea 100644 --- a/api/rag/document.pb.go +++ b/api/rag/document.pb.go @@ -29,7 +29,7 @@ type CreateDocumentRequest struct { Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` Content string `protobuf:"bytes,2,opt,name=content,proto3" json:"content,omitempty"` - LibraryId string `protobuf:"bytes,3,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` + LibraryId int64 `protobuf:"varint,3,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` } func (x *CreateDocumentRequest) Reset() { @@ -78,11 +78,11 @@ func (x *CreateDocumentRequest) GetContent() string { return "" } -func (x *CreateDocumentRequest) GetLibraryId() string { +func (x *CreateDocumentRequest) GetLibraryId() int64 { if x != nil { return x.LibraryId } - return "" + return 0 } type CreateDocumentResponse struct { @@ -137,7 +137,7 @@ type ListDocumentRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - LibraryId string `protobuf:"bytes,1,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` + LibraryId int64 `protobuf:"varint,1,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` } func (x *ListDocumentRequest) Reset() { @@ -172,11 +172,11 @@ func (*ListDocumentRequest) Descriptor() ([]byte, []int) { return file_api_rag_document_proto_rawDescGZIP(), []int{2} } -func (x *ListDocumentRequest) GetLibraryId() string { +func (x *ListDocumentRequest) GetLibraryId() int64 { if x != nil { return x.LibraryId } - return "" + return 0 } type ListDocumentResponse struct { @@ -234,7 +234,7 @@ type Document struct { Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` Content string `protobuf:"bytes,3,opt,name=content,proto3" json:"content,omitempty"` - LibraryId string `protobuf:"bytes,4,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` + LibraryId int64 `protobuf:"varint,4,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` UserId string `protobuf:"bytes,5,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` CreatedAt string `protobuf:"bytes,6,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` UpdatedAt string `protobuf:"bytes,7,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` @@ -293,11 +293,11 @@ func (x *Document) GetContent() string { return "" } -func (x *Document) GetLibraryId() string { +func (x *Document) GetLibraryId() int64 { if x != nil { return x.LibraryId } - return "" + return 0 } func (x *Document) GetUserId() string { @@ -334,7 +334,7 @@ var file_api_rag_document_proto_rawDesc = []byte{ 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6c, 0x69, 0x62, 0x72, - 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x69, + 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x22, 0x3f, 0x0a, 0x16, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x25, 0x0a, 0x08, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, @@ -342,7 +342,7 @@ var file_api_rag_document_proto_rawDesc = []byte{ 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x34, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x22, 0x3f, + 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x22, 0x3f, 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, 0x0a, 0x09, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x44, 0x6f, 0x63, 0x75, @@ -352,7 +352,7 @@ var file_api_rag_document_proto_rawDesc = []byte{ 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6c, 0x69, - 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, + 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, diff --git a/api/rag/document.proto b/api/rag/document.proto index 0f31deb..9879412 100644 --- a/api/rag/document.proto +++ b/api/rag/document.proto @@ -9,7 +9,7 @@ option go_package = "leafdev.top/leaf/rag/api/rag"; message CreateDocumentRequest { string name = 1; string content = 2; - string library_id = 3; + int64 library_id = 3; } message CreateDocumentResponse { @@ -17,19 +17,32 @@ message CreateDocumentResponse { } message ListDocumentRequest { - string library_id = 1; + int64 library_id = 1; } message ListDocumentResponse { repeated Document documents = 1; } +message CreateDocumentBlockRequest { + int64 document_id = 1; + string content = 2; +} + message Document { int64 id = 1; string name = 2; string content = 3; - string library_id = 4; + int64 library_id = 4; string user_id = 5; string created_at = 6; string updated_at = 7; +} + +message DocumentBlock { + int64 id = 1; + string content = 2; + int64 document_id = 3; + string created_at = 4; + string updated_at = 5; } \ No newline at end of file diff --git a/api/rag/rag.swagger.json b/api/rag/rag.swagger.json index dbf24a3..74c9cab 100644 --- a/api/rag/rag.swagger.json +++ b/api/rag/rag.swagger.json @@ -38,7 +38,8 @@ "name": "libraryId", "in": "query", "required": false, - "type": "string" + "type": "string", + "format": "int64" } ], "tags": [ @@ -79,7 +80,8 @@ "name": "libraryId", "in": "query", "required": false, - "type": "string" + "type": "string", + "format": "int64" } ], "tags": [ @@ -178,7 +180,8 @@ "type": "string" }, "libraryId": { - "type": "string" + "type": "string", + "format": "int64" }, "userId": { "type": "string" diff --git a/api/rag/rag_grpc.pb.go b/api/rag/rag_grpc.pb.go index c2362bd..a7283c6 100644 --- a/api/rag/rag_grpc.pb.go +++ b/api/rag/rag_grpc.pb.go @@ -24,7 +24,7 @@ const _ = grpc.SupportPackageIsVersion7 type RAGServiceClient interface { ListLibrary(ctx context.Context, in *ListLibraryRequest, opts ...grpc.CallOption) (*ListLibraryResponse, error) CreateLibrary(ctx context.Context, in *CreateLibraryRequest, opts ...grpc.CallOption) (*CreateLibraryResponse, error) - // DocumentService + // DocumentService CreateDocument(ctx context.Context, in *CreateDocumentRequest, opts ...grpc.CallOption) (*CreateDocumentResponse, error) ListDocument(ctx context.Context, in *ListDocumentRequest, opts ...grpc.CallOption) (*ListDocumentResponse, error) } @@ -79,7 +79,7 @@ func (c *rAGServiceClient) ListDocument(ctx context.Context, in *ListDocumentReq type RAGServiceServer interface { ListLibrary(context.Context, *ListLibraryRequest) (*ListLibraryResponse, error) CreateLibrary(context.Context, *CreateLibraryRequest) (*CreateLibraryResponse, error) - // DocumentService + // DocumentService CreateDocument(context.Context, *CreateDocumentRequest) (*CreateDocumentResponse, error) ListDocument(context.Context, *ListDocumentRequest) (*ListDocumentResponse, error) mustEmbedUnimplementedRAGServiceServer() diff --git a/ent/document.go b/ent/document.go index 45a0a95..d7b50fe 100644 --- a/ent/document.go +++ b/ent/document.go @@ -21,6 +21,8 @@ type Document struct { Name string `json:"name,omitempty"` // UserID holds the value of the "user_id" field. UserID string `json:"user_id,omitempty"` + // LibraryID holds the value of the "library_id" field. + LibraryID int64 `json:"library_id,omitempty"` // CreatedAt holds the value of the "created_at" field. CreatedAt time.Time `json:"created_at,omitempty"` // UpdatedAt holds the value of the "updated_at" field. @@ -34,7 +36,7 @@ func (*Document) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case document.FieldID: + case document.FieldID, document.FieldLibraryID: values[i] = new(sql.NullInt64) case document.FieldName, document.FieldUserID: values[i] = new(sql.NullString) @@ -75,6 +77,12 @@ func (d *Document) assignValues(columns []string, values []any) error { } else if value.Valid { d.UserID = value.String } + case document.FieldLibraryID: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field library_id", values[i]) + } else if value.Valid { + d.LibraryID = value.Int64 + } case document.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field created_at", values[i]) @@ -136,6 +144,9 @@ func (d *Document) String() string { builder.WriteString("user_id=") builder.WriteString(d.UserID) builder.WriteString(", ") + builder.WriteString("library_id=") + builder.WriteString(fmt.Sprintf("%v", d.LibraryID)) + builder.WriteString(", ") builder.WriteString("created_at=") builder.WriteString(d.CreatedAt.Format(time.ANSIC)) builder.WriteString(", ") diff --git a/ent/document/document.go b/ent/document/document.go index dd91161..fe7c291 100644 --- a/ent/document/document.go +++ b/ent/document/document.go @@ -17,6 +17,8 @@ const ( FieldName = "name" // FieldUserID holds the string denoting the user_id field in the database. FieldUserID = "user_id" + // FieldLibraryID holds the string denoting the library_id field in the database. + FieldLibraryID = "library_id" // FieldCreatedAt holds the string denoting the created_at field in the database. FieldCreatedAt = "created_at" // FieldUpdatedAt holds the string denoting the updated_at field in the database. @@ -30,6 +32,7 @@ var Columns = []string{ FieldID, FieldName, FieldUserID, + FieldLibraryID, FieldCreatedAt, FieldUpdatedAt, } @@ -80,6 +83,11 @@ func ByUserID(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldUserID, opts...).ToFunc() } +// ByLibraryID orders the results by the library_id field. +func ByLibraryID(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldLibraryID, opts...).ToFunc() +} + // ByCreatedAt orders the results by the created_at field. func ByCreatedAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldCreatedAt, opts...).ToFunc() diff --git a/ent/document/where.go b/ent/document/where.go index fca7ff3..06c7e24 100644 --- a/ent/document/where.go +++ b/ent/document/where.go @@ -64,6 +64,11 @@ func UserID(v string) predicate.Document { return predicate.Document(sql.FieldEQ(FieldUserID, v)) } +// LibraryID applies equality check predicate on the "library_id" field. It's identical to LibraryIDEQ. +func LibraryID(v int64) predicate.Document { + return predicate.Document(sql.FieldEQ(FieldLibraryID, v)) +} + // CreatedAt applies equality check predicate on the "created_at" field. It's identical to CreatedAtEQ. func CreatedAt(v time.Time) predicate.Document { return predicate.Document(sql.FieldEQ(FieldCreatedAt, v)) @@ -204,6 +209,46 @@ func UserIDContainsFold(v string) predicate.Document { return predicate.Document(sql.FieldContainsFold(FieldUserID, v)) } +// LibraryIDEQ applies the EQ predicate on the "library_id" field. +func LibraryIDEQ(v int64) predicate.Document { + return predicate.Document(sql.FieldEQ(FieldLibraryID, v)) +} + +// LibraryIDNEQ applies the NEQ predicate on the "library_id" field. +func LibraryIDNEQ(v int64) predicate.Document { + return predicate.Document(sql.FieldNEQ(FieldLibraryID, v)) +} + +// LibraryIDIn applies the In predicate on the "library_id" field. +func LibraryIDIn(vs ...int64) predicate.Document { + return predicate.Document(sql.FieldIn(FieldLibraryID, vs...)) +} + +// LibraryIDNotIn applies the NotIn predicate on the "library_id" field. +func LibraryIDNotIn(vs ...int64) predicate.Document { + return predicate.Document(sql.FieldNotIn(FieldLibraryID, vs...)) +} + +// LibraryIDGT applies the GT predicate on the "library_id" field. +func LibraryIDGT(v int64) predicate.Document { + return predicate.Document(sql.FieldGT(FieldLibraryID, v)) +} + +// LibraryIDGTE applies the GTE predicate on the "library_id" field. +func LibraryIDGTE(v int64) predicate.Document { + return predicate.Document(sql.FieldGTE(FieldLibraryID, v)) +} + +// LibraryIDLT applies the LT predicate on the "library_id" field. +func LibraryIDLT(v int64) predicate.Document { + return predicate.Document(sql.FieldLT(FieldLibraryID, v)) +} + +// LibraryIDLTE applies the LTE predicate on the "library_id" field. +func LibraryIDLTE(v int64) predicate.Document { + return predicate.Document(sql.FieldLTE(FieldLibraryID, v)) +} + // CreatedAtEQ applies the EQ predicate on the "created_at" field. func CreatedAtEQ(v time.Time) predicate.Document { return predicate.Document(sql.FieldEQ(FieldCreatedAt, v)) diff --git a/ent/document_create.go b/ent/document_create.go index 1db501f..661173a 100644 --- a/ent/document_create.go +++ b/ent/document_create.go @@ -32,6 +32,12 @@ func (dc *DocumentCreate) SetUserID(s string) *DocumentCreate { return dc } +// SetLibraryID sets the "library_id" field. +func (dc *DocumentCreate) SetLibraryID(i int64) *DocumentCreate { + dc.mutation.SetLibraryID(i) + return dc +} + // SetCreatedAt sets the "created_at" field. func (dc *DocumentCreate) SetCreatedAt(t time.Time) *DocumentCreate { dc.mutation.SetCreatedAt(t) @@ -113,6 +119,9 @@ func (dc *DocumentCreate) check() error { if _, ok := dc.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "Document.user_id"`)} } + if _, ok := dc.mutation.LibraryID(); !ok { + return &ValidationError{Name: "library_id", err: errors.New(`ent: missing required field "Document.library_id"`)} + } if _, ok := dc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Document.created_at"`)} } @@ -153,6 +162,10 @@ func (dc *DocumentCreate) createSpec() (*Document, *sqlgraph.CreateSpec) { _spec.SetField(document.FieldUserID, field.TypeString, value) _node.UserID = value } + if value, ok := dc.mutation.LibraryID(); ok { + _spec.SetField(document.FieldLibraryID, field.TypeInt64, value) + _node.LibraryID = value + } if value, ok := dc.mutation.CreatedAt(); ok { _spec.SetField(document.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value diff --git a/ent/document_update.go b/ent/document_update.go index f9067a1..3dbdc11 100644 --- a/ent/document_update.go +++ b/ent/document_update.go @@ -56,6 +56,27 @@ func (du *DocumentUpdate) SetNillableUserID(s *string) *DocumentUpdate { return du } +// SetLibraryID sets the "library_id" field. +func (du *DocumentUpdate) SetLibraryID(i int64) *DocumentUpdate { + du.mutation.ResetLibraryID() + du.mutation.SetLibraryID(i) + return du +} + +// SetNillableLibraryID sets the "library_id" field if the given value is not nil. +func (du *DocumentUpdate) SetNillableLibraryID(i *int64) *DocumentUpdate { + if i != nil { + du.SetLibraryID(*i) + } + return du +} + +// AddLibraryID adds i to the "library_id" field. +func (du *DocumentUpdate) AddLibraryID(i int64) *DocumentUpdate { + du.mutation.AddLibraryID(i) + return du +} + // SetCreatedAt sets the "created_at" field. func (du *DocumentUpdate) SetCreatedAt(t time.Time) *DocumentUpdate { du.mutation.SetCreatedAt(t) @@ -131,6 +152,12 @@ func (du *DocumentUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := du.mutation.UserID(); ok { _spec.SetField(document.FieldUserID, field.TypeString, value) } + if value, ok := du.mutation.LibraryID(); ok { + _spec.SetField(document.FieldLibraryID, field.TypeInt64, value) + } + if value, ok := du.mutation.AddedLibraryID(); ok { + _spec.AddField(document.FieldLibraryID, field.TypeInt64, value) + } if value, ok := du.mutation.CreatedAt(); ok { _spec.SetField(document.FieldCreatedAt, field.TypeTime, value) } @@ -185,6 +212,27 @@ func (duo *DocumentUpdateOne) SetNillableUserID(s *string) *DocumentUpdateOne { return duo } +// SetLibraryID sets the "library_id" field. +func (duo *DocumentUpdateOne) SetLibraryID(i int64) *DocumentUpdateOne { + duo.mutation.ResetLibraryID() + duo.mutation.SetLibraryID(i) + return duo +} + +// SetNillableLibraryID sets the "library_id" field if the given value is not nil. +func (duo *DocumentUpdateOne) SetNillableLibraryID(i *int64) *DocumentUpdateOne { + if i != nil { + duo.SetLibraryID(*i) + } + return duo +} + +// AddLibraryID adds i to the "library_id" field. +func (duo *DocumentUpdateOne) AddLibraryID(i int64) *DocumentUpdateOne { + duo.mutation.AddLibraryID(i) + return duo +} + // SetCreatedAt sets the "created_at" field. func (duo *DocumentUpdateOne) SetCreatedAt(t time.Time) *DocumentUpdateOne { duo.mutation.SetCreatedAt(t) @@ -290,6 +338,12 @@ func (duo *DocumentUpdateOne) sqlSave(ctx context.Context) (_node *Document, err if value, ok := duo.mutation.UserID(); ok { _spec.SetField(document.FieldUserID, field.TypeString, value) } + if value, ok := duo.mutation.LibraryID(); ok { + _spec.SetField(document.FieldLibraryID, field.TypeInt64, value) + } + if value, ok := duo.mutation.AddedLibraryID(); ok { + _spec.AddField(document.FieldLibraryID, field.TypeInt64, value) + } if value, ok := duo.mutation.CreatedAt(); ok { _spec.SetField(document.FieldCreatedAt, field.TypeTime, value) } diff --git a/ent/documentblock.go b/ent/documentblock.go index d9fc570..c788e15 100644 --- a/ent/documentblock.go +++ b/ent/documentblock.go @@ -22,9 +22,9 @@ type DocumentBlock struct { // Content holds the value of the "content" field. Content string `json:"content,omitempty"` // Order holds the value of the "order" field. - Order int `json:"order,omitempty"` + Order int64 `json:"order,omitempty"` // DocumentID holds the value of the "document_id" field. - DocumentID int `json:"document_id,omitempty"` + DocumentID int64 `json:"document_id,omitempty"` // UserID holds the value of the "user_id" field. UserID string `json:"user_id,omitempty"` // CreatedAt holds the value of the "created_at" field. @@ -103,13 +103,13 @@ func (db *DocumentBlock) assignValues(columns []string, values []any) error { if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field order", values[i]) } else if value.Valid { - db.Order = int(value.Int64) + db.Order = value.Int64 } case documentblock.FieldDocumentID: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field document_id", values[i]) } else if value.Valid { - db.DocumentID = int(value.Int64) + db.DocumentID = value.Int64 } case documentblock.FieldUserID: if value, ok := values[i].(*sql.NullString); !ok { diff --git a/ent/documentblock/where.go b/ent/documentblock/where.go index c5e96bc..0ea7bc0 100644 --- a/ent/documentblock/where.go +++ b/ent/documentblock/where.go @@ -66,12 +66,12 @@ func Content(v string) predicate.DocumentBlock { } // Order applies equality check predicate on the "order" field. It's identical to OrderEQ. -func Order(v int) predicate.DocumentBlock { +func Order(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldEQ(FieldOrder, v)) } // DocumentID applies equality check predicate on the "document_id" field. It's identical to DocumentIDEQ. -func DocumentID(v int) predicate.DocumentBlock { +func DocumentID(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldEQ(FieldDocumentID, v)) } @@ -221,82 +221,82 @@ func ContentContainsFold(v string) predicate.DocumentBlock { } // OrderEQ applies the EQ predicate on the "order" field. -func OrderEQ(v int) predicate.DocumentBlock { +func OrderEQ(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldEQ(FieldOrder, v)) } // OrderNEQ applies the NEQ predicate on the "order" field. -func OrderNEQ(v int) predicate.DocumentBlock { +func OrderNEQ(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldNEQ(FieldOrder, v)) } // OrderIn applies the In predicate on the "order" field. -func OrderIn(vs ...int) predicate.DocumentBlock { +func OrderIn(vs ...int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldIn(FieldOrder, vs...)) } // OrderNotIn applies the NotIn predicate on the "order" field. -func OrderNotIn(vs ...int) predicate.DocumentBlock { +func OrderNotIn(vs ...int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldNotIn(FieldOrder, vs...)) } // OrderGT applies the GT predicate on the "order" field. -func OrderGT(v int) predicate.DocumentBlock { +func OrderGT(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldGT(FieldOrder, v)) } // OrderGTE applies the GTE predicate on the "order" field. -func OrderGTE(v int) predicate.DocumentBlock { +func OrderGTE(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldGTE(FieldOrder, v)) } // OrderLT applies the LT predicate on the "order" field. -func OrderLT(v int) predicate.DocumentBlock { +func OrderLT(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldLT(FieldOrder, v)) } // OrderLTE applies the LTE predicate on the "order" field. -func OrderLTE(v int) predicate.DocumentBlock { +func OrderLTE(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldLTE(FieldOrder, v)) } // DocumentIDEQ applies the EQ predicate on the "document_id" field. -func DocumentIDEQ(v int) predicate.DocumentBlock { +func DocumentIDEQ(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldEQ(FieldDocumentID, v)) } // DocumentIDNEQ applies the NEQ predicate on the "document_id" field. -func DocumentIDNEQ(v int) predicate.DocumentBlock { +func DocumentIDNEQ(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldNEQ(FieldDocumentID, v)) } // DocumentIDIn applies the In predicate on the "document_id" field. -func DocumentIDIn(vs ...int) predicate.DocumentBlock { +func DocumentIDIn(vs ...int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldIn(FieldDocumentID, vs...)) } // DocumentIDNotIn applies the NotIn predicate on the "document_id" field. -func DocumentIDNotIn(vs ...int) predicate.DocumentBlock { +func DocumentIDNotIn(vs ...int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldNotIn(FieldDocumentID, vs...)) } // DocumentIDGT applies the GT predicate on the "document_id" field. -func DocumentIDGT(v int) predicate.DocumentBlock { +func DocumentIDGT(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldGT(FieldDocumentID, v)) } // DocumentIDGTE applies the GTE predicate on the "document_id" field. -func DocumentIDGTE(v int) predicate.DocumentBlock { +func DocumentIDGTE(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldGTE(FieldDocumentID, v)) } // DocumentIDLT applies the LT predicate on the "document_id" field. -func DocumentIDLT(v int) predicate.DocumentBlock { +func DocumentIDLT(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldLT(FieldDocumentID, v)) } // DocumentIDLTE applies the LTE predicate on the "document_id" field. -func DocumentIDLTE(v int) predicate.DocumentBlock { +func DocumentIDLTE(v int64) predicate.DocumentBlock { return predicate.DocumentBlock(sql.FieldLTE(FieldDocumentID, v)) } diff --git a/ent/documentblock_create.go b/ent/documentblock_create.go index 53fa492..891277d 100644 --- a/ent/documentblock_create.go +++ b/ent/documentblock_create.go @@ -34,13 +34,13 @@ func (dbc *DocumentBlockCreate) SetContent(s string) *DocumentBlockCreate { } // SetOrder sets the "order" field. -func (dbc *DocumentBlockCreate) SetOrder(i int) *DocumentBlockCreate { +func (dbc *DocumentBlockCreate) SetOrder(i int64) *DocumentBlockCreate { dbc.mutation.SetOrder(i) return dbc } // SetDocumentID sets the "document_id" field. -func (dbc *DocumentBlockCreate) SetDocumentID(i int) *DocumentBlockCreate { +func (dbc *DocumentBlockCreate) SetDocumentID(i int64) *DocumentBlockCreate { dbc.mutation.SetDocumentID(i) return dbc } @@ -197,11 +197,11 @@ func (dbc *DocumentBlockCreate) createSpec() (*DocumentBlock, *sqlgraph.CreateSp _node.Content = value } if value, ok := dbc.mutation.Order(); ok { - _spec.SetField(documentblock.FieldOrder, field.TypeInt, value) + _spec.SetField(documentblock.FieldOrder, field.TypeInt64, value) _node.Order = value } if value, ok := dbc.mutation.DocumentID(); ok { - _spec.SetField(documentblock.FieldDocumentID, field.TypeInt, value) + _spec.SetField(documentblock.FieldDocumentID, field.TypeInt64, value) _node.DocumentID = value } if value, ok := dbc.mutation.UserID(); ok { diff --git a/ent/documentblock_update.go b/ent/documentblock_update.go index a659f12..f283626 100644 --- a/ent/documentblock_update.go +++ b/ent/documentblock_update.go @@ -58,14 +58,14 @@ func (dbu *DocumentBlockUpdate) SetNillableContent(s *string) *DocumentBlockUpda } // SetOrder sets the "order" field. -func (dbu *DocumentBlockUpdate) SetOrder(i int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) SetOrder(i int64) *DocumentBlockUpdate { dbu.mutation.ResetOrder() dbu.mutation.SetOrder(i) return dbu } // SetNillableOrder sets the "order" field if the given value is not nil. -func (dbu *DocumentBlockUpdate) SetNillableOrder(i *int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) SetNillableOrder(i *int64) *DocumentBlockUpdate { if i != nil { dbu.SetOrder(*i) } @@ -73,20 +73,20 @@ func (dbu *DocumentBlockUpdate) SetNillableOrder(i *int) *DocumentBlockUpdate { } // AddOrder adds i to the "order" field. -func (dbu *DocumentBlockUpdate) AddOrder(i int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) AddOrder(i int64) *DocumentBlockUpdate { dbu.mutation.AddOrder(i) return dbu } // SetDocumentID sets the "document_id" field. -func (dbu *DocumentBlockUpdate) SetDocumentID(i int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) SetDocumentID(i int64) *DocumentBlockUpdate { dbu.mutation.ResetDocumentID() dbu.mutation.SetDocumentID(i) return dbu } // SetNillableDocumentID sets the "document_id" field if the given value is not nil. -func (dbu *DocumentBlockUpdate) SetNillableDocumentID(i *int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) SetNillableDocumentID(i *int64) *DocumentBlockUpdate { if i != nil { dbu.SetDocumentID(*i) } @@ -94,7 +94,7 @@ func (dbu *DocumentBlockUpdate) SetNillableDocumentID(i *int) *DocumentBlockUpda } // AddDocumentID adds i to the "document_id" field. -func (dbu *DocumentBlockUpdate) AddDocumentID(i int) *DocumentBlockUpdate { +func (dbu *DocumentBlockUpdate) AddDocumentID(i int64) *DocumentBlockUpdate { dbu.mutation.AddDocumentID(i) return dbu } @@ -225,16 +225,16 @@ func (dbu *DocumentBlockUpdate) sqlSave(ctx context.Context) (n int, err error) _spec.SetField(documentblock.FieldContent, field.TypeString, value) } if value, ok := dbu.mutation.Order(); ok { - _spec.SetField(documentblock.FieldOrder, field.TypeInt, value) + _spec.SetField(documentblock.FieldOrder, field.TypeInt64, value) } if value, ok := dbu.mutation.AddedOrder(); ok { - _spec.AddField(documentblock.FieldOrder, field.TypeInt, value) + _spec.AddField(documentblock.FieldOrder, field.TypeInt64, value) } if value, ok := dbu.mutation.DocumentID(); ok { - _spec.SetField(documentblock.FieldDocumentID, field.TypeInt, value) + _spec.SetField(documentblock.FieldDocumentID, field.TypeInt64, value) } if value, ok := dbu.mutation.AddedDocumentID(); ok { - _spec.AddField(documentblock.FieldDocumentID, field.TypeInt, value) + _spec.AddField(documentblock.FieldDocumentID, field.TypeInt64, value) } if value, ok := dbu.mutation.UserID(); ok { _spec.SetField(documentblock.FieldUserID, field.TypeString, value) @@ -339,14 +339,14 @@ func (dbuo *DocumentBlockUpdateOne) SetNillableContent(s *string) *DocumentBlock } // SetOrder sets the "order" field. -func (dbuo *DocumentBlockUpdateOne) SetOrder(i int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) SetOrder(i int64) *DocumentBlockUpdateOne { dbuo.mutation.ResetOrder() dbuo.mutation.SetOrder(i) return dbuo } // SetNillableOrder sets the "order" field if the given value is not nil. -func (dbuo *DocumentBlockUpdateOne) SetNillableOrder(i *int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) SetNillableOrder(i *int64) *DocumentBlockUpdateOne { if i != nil { dbuo.SetOrder(*i) } @@ -354,20 +354,20 @@ func (dbuo *DocumentBlockUpdateOne) SetNillableOrder(i *int) *DocumentBlockUpdat } // AddOrder adds i to the "order" field. -func (dbuo *DocumentBlockUpdateOne) AddOrder(i int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) AddOrder(i int64) *DocumentBlockUpdateOne { dbuo.mutation.AddOrder(i) return dbuo } // SetDocumentID sets the "document_id" field. -func (dbuo *DocumentBlockUpdateOne) SetDocumentID(i int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) SetDocumentID(i int64) *DocumentBlockUpdateOne { dbuo.mutation.ResetDocumentID() dbuo.mutation.SetDocumentID(i) return dbuo } // SetNillableDocumentID sets the "document_id" field if the given value is not nil. -func (dbuo *DocumentBlockUpdateOne) SetNillableDocumentID(i *int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) SetNillableDocumentID(i *int64) *DocumentBlockUpdateOne { if i != nil { dbuo.SetDocumentID(*i) } @@ -375,7 +375,7 @@ func (dbuo *DocumentBlockUpdateOne) SetNillableDocumentID(i *int) *DocumentBlock } // AddDocumentID adds i to the "document_id" field. -func (dbuo *DocumentBlockUpdateOne) AddDocumentID(i int) *DocumentBlockUpdateOne { +func (dbuo *DocumentBlockUpdateOne) AddDocumentID(i int64) *DocumentBlockUpdateOne { dbuo.mutation.AddDocumentID(i) return dbuo } @@ -536,16 +536,16 @@ func (dbuo *DocumentBlockUpdateOne) sqlSave(ctx context.Context) (_node *Documen _spec.SetField(documentblock.FieldContent, field.TypeString, value) } if value, ok := dbuo.mutation.Order(); ok { - _spec.SetField(documentblock.FieldOrder, field.TypeInt, value) + _spec.SetField(documentblock.FieldOrder, field.TypeInt64, value) } if value, ok := dbuo.mutation.AddedOrder(); ok { - _spec.AddField(documentblock.FieldOrder, field.TypeInt, value) + _spec.AddField(documentblock.FieldOrder, field.TypeInt64, value) } if value, ok := dbuo.mutation.DocumentID(); ok { - _spec.SetField(documentblock.FieldDocumentID, field.TypeInt, value) + _spec.SetField(documentblock.FieldDocumentID, field.TypeInt64, value) } if value, ok := dbuo.mutation.AddedDocumentID(); ok { - _spec.AddField(documentblock.FieldDocumentID, field.TypeInt, value) + _spec.AddField(documentblock.FieldDocumentID, field.TypeInt64, value) } if value, ok := dbuo.mutation.UserID(); ok { _spec.SetField(documentblock.FieldUserID, field.TypeString, value) diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index 375f26a..1f31c04 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -13,6 +13,7 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "name", Type: field.TypeString}, {Name: "user_id", Type: field.TypeString}, + {Name: "library_id", Type: field.TypeInt64}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "document_block_document", Type: field.TypeInt, Nullable: true}, @@ -25,7 +26,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "documents_document_blocks_document", - Columns: []*schema.Column{DocumentsColumns[5]}, + Columns: []*schema.Column{DocumentsColumns[6]}, RefColumns: []*schema.Column{DocumentBlocksColumns[0]}, OnDelete: schema.SetNull, }, @@ -43,8 +44,8 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "name", Type: field.TypeString}, {Name: "content", Type: field.TypeString}, - {Name: "order", Type: field.TypeInt}, - {Name: "document_id", Type: field.TypeInt}, + {Name: "order", Type: field.TypeInt64}, + {Name: "document_id", Type: field.TypeInt64}, {Name: "user_id", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, diff --git a/ent/mutation.go b/ent/mutation.go index 244a4d1..2e7d616 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -39,6 +39,8 @@ type DocumentMutation struct { id *int name *string user_id *string + library_id *int64 + addlibrary_id *int64 created_at *time.Time updated_at *time.Time clearedFields map[string]struct{} @@ -217,6 +219,62 @@ func (m *DocumentMutation) ResetUserID() { m.user_id = nil } +// SetLibraryID sets the "library_id" field. +func (m *DocumentMutation) SetLibraryID(i int64) { + m.library_id = &i + m.addlibrary_id = nil +} + +// LibraryID returns the value of the "library_id" field in the mutation. +func (m *DocumentMutation) LibraryID() (r int64, exists bool) { + v := m.library_id + if v == nil { + return + } + return *v, true +} + +// OldLibraryID returns the old "library_id" field's value of the Document entity. +// If the Document object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentMutation) OldLibraryID(ctx context.Context) (v int64, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldLibraryID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldLibraryID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldLibraryID: %w", err) + } + return oldValue.LibraryID, nil +} + +// AddLibraryID adds i to the "library_id" field. +func (m *DocumentMutation) AddLibraryID(i int64) { + if m.addlibrary_id != nil { + *m.addlibrary_id += i + } else { + m.addlibrary_id = &i + } +} + +// AddedLibraryID returns the value that was added to the "library_id" field in this mutation. +func (m *DocumentMutation) AddedLibraryID() (r int64, exists bool) { + v := m.addlibrary_id + if v == nil { + return + } + return *v, true +} + +// ResetLibraryID resets all changes to the "library_id" field. +func (m *DocumentMutation) ResetLibraryID() { + m.library_id = nil + m.addlibrary_id = nil +} + // SetCreatedAt sets the "created_at" field. func (m *DocumentMutation) SetCreatedAt(t time.Time) { m.created_at = &t @@ -323,13 +381,16 @@ func (m *DocumentMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *DocumentMutation) Fields() []string { - fields := make([]string, 0, 4) + fields := make([]string, 0, 5) if m.name != nil { fields = append(fields, document.FieldName) } if m.user_id != nil { fields = append(fields, document.FieldUserID) } + if m.library_id != nil { + fields = append(fields, document.FieldLibraryID) + } if m.created_at != nil { fields = append(fields, document.FieldCreatedAt) } @@ -348,6 +409,8 @@ func (m *DocumentMutation) Field(name string) (ent.Value, bool) { return m.Name() case document.FieldUserID: return m.UserID() + case document.FieldLibraryID: + return m.LibraryID() case document.FieldCreatedAt: return m.CreatedAt() case document.FieldUpdatedAt: @@ -365,6 +428,8 @@ func (m *DocumentMutation) OldField(ctx context.Context, name string) (ent.Value return m.OldName(ctx) case document.FieldUserID: return m.OldUserID(ctx) + case document.FieldLibraryID: + return m.OldLibraryID(ctx) case document.FieldCreatedAt: return m.OldCreatedAt(ctx) case document.FieldUpdatedAt: @@ -392,6 +457,13 @@ func (m *DocumentMutation) SetField(name string, value ent.Value) error { } m.SetUserID(v) return nil + case document.FieldLibraryID: + v, ok := value.(int64) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetLibraryID(v) + return nil case document.FieldCreatedAt: v, ok := value.(time.Time) if !ok { @@ -413,13 +485,21 @@ func (m *DocumentMutation) SetField(name string, value ent.Value) error { // AddedFields returns all numeric fields that were incremented/decremented during // this mutation. func (m *DocumentMutation) AddedFields() []string { - return nil + var fields []string + if m.addlibrary_id != nil { + fields = append(fields, document.FieldLibraryID) + } + return fields } // AddedField returns the numeric value that was incremented/decremented on a field // with the given name. The second boolean return value indicates that this field // was not set, or was not defined in the schema. func (m *DocumentMutation) AddedField(name string) (ent.Value, bool) { + switch name { + case document.FieldLibraryID: + return m.AddedLibraryID() + } return nil, false } @@ -428,6 +508,13 @@ func (m *DocumentMutation) AddedField(name string) (ent.Value, bool) { // type. func (m *DocumentMutation) AddField(name string, value ent.Value) error { switch name { + case document.FieldLibraryID: + v, ok := value.(int64) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddLibraryID(v) + return nil } return fmt.Errorf("unknown Document numeric field %s", name) } @@ -461,6 +548,9 @@ func (m *DocumentMutation) ResetField(name string) error { case document.FieldUserID: m.ResetUserID() return nil + case document.FieldLibraryID: + m.ResetLibraryID() + return nil case document.FieldCreatedAt: m.ResetCreatedAt() return nil @@ -527,10 +617,10 @@ type DocumentBlockMutation struct { id *int name *string content *string - _order *int - add_order *int - document_id *int - adddocument_id *int + _order *int64 + add_order *int64 + document_id *int64 + adddocument_id *int64 user_id *string created_at *time.Time updated_at *time.Time @@ -714,13 +804,13 @@ func (m *DocumentBlockMutation) ResetContent() { } // SetOrder sets the "order" field. -func (m *DocumentBlockMutation) SetOrder(i int) { +func (m *DocumentBlockMutation) SetOrder(i int64) { m._order = &i m.add_order = nil } // Order returns the value of the "order" field in the mutation. -func (m *DocumentBlockMutation) Order() (r int, exists bool) { +func (m *DocumentBlockMutation) Order() (r int64, exists bool) { v := m._order if v == nil { return @@ -731,7 +821,7 @@ func (m *DocumentBlockMutation) Order() (r int, exists bool) { // OldOrder returns the old "order" field's value of the DocumentBlock entity. // If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *DocumentBlockMutation) OldOrder(ctx context.Context) (v int, err error) { +func (m *DocumentBlockMutation) OldOrder(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldOrder is only allowed on UpdateOne operations") } @@ -746,7 +836,7 @@ func (m *DocumentBlockMutation) OldOrder(ctx context.Context) (v int, err error) } // AddOrder adds i to the "order" field. -func (m *DocumentBlockMutation) AddOrder(i int) { +func (m *DocumentBlockMutation) AddOrder(i int64) { if m.add_order != nil { *m.add_order += i } else { @@ -755,7 +845,7 @@ func (m *DocumentBlockMutation) AddOrder(i int) { } // AddedOrder returns the value that was added to the "order" field in this mutation. -func (m *DocumentBlockMutation) AddedOrder() (r int, exists bool) { +func (m *DocumentBlockMutation) AddedOrder() (r int64, exists bool) { v := m.add_order if v == nil { return @@ -770,13 +860,13 @@ func (m *DocumentBlockMutation) ResetOrder() { } // SetDocumentID sets the "document_id" field. -func (m *DocumentBlockMutation) SetDocumentID(i int) { +func (m *DocumentBlockMutation) SetDocumentID(i int64) { m.document_id = &i m.adddocument_id = nil } // DocumentID returns the value of the "document_id" field in the mutation. -func (m *DocumentBlockMutation) DocumentID() (r int, exists bool) { +func (m *DocumentBlockMutation) DocumentID() (r int64, exists bool) { v := m.document_id if v == nil { return @@ -787,7 +877,7 @@ func (m *DocumentBlockMutation) DocumentID() (r int, exists bool) { // OldDocumentID returns the old "document_id" field's value of the DocumentBlock entity. // If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. // An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *DocumentBlockMutation) OldDocumentID(ctx context.Context) (v int, err error) { +func (m *DocumentBlockMutation) OldDocumentID(ctx context.Context) (v int64, err error) { if !m.op.Is(OpUpdateOne) { return v, errors.New("OldDocumentID is only allowed on UpdateOne operations") } @@ -802,7 +892,7 @@ func (m *DocumentBlockMutation) OldDocumentID(ctx context.Context) (v int, err e } // AddDocumentID adds i to the "document_id" field. -func (m *DocumentBlockMutation) AddDocumentID(i int) { +func (m *DocumentBlockMutation) AddDocumentID(i int64) { if m.adddocument_id != nil { *m.adddocument_id += i } else { @@ -811,7 +901,7 @@ func (m *DocumentBlockMutation) AddDocumentID(i int) { } // AddedDocumentID returns the value that was added to the "document_id" field in this mutation. -func (m *DocumentBlockMutation) AddedDocumentID() (r int, exists bool) { +func (m *DocumentBlockMutation) AddedDocumentID() (r int64, exists bool) { v := m.adddocument_id if v == nil { return @@ -1112,14 +1202,14 @@ func (m *DocumentBlockMutation) SetField(name string, value ent.Value) error { m.SetContent(v) return nil case documentblock.FieldOrder: - v, ok := value.(int) + v, ok := value.(int64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } m.SetOrder(v) return nil case documentblock.FieldDocumentID: - v, ok := value.(int) + v, ok := value.(int64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } @@ -1182,14 +1272,14 @@ func (m *DocumentBlockMutation) AddedField(name string) (ent.Value, bool) { func (m *DocumentBlockMutation) AddField(name string, value ent.Value) error { switch name { case documentblock.FieldOrder: - v, ok := value.(int) + v, ok := value.(int64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } m.AddOrder(v) return nil case documentblock.FieldDocumentID: - v, ok := value.(int) + v, ok := value.(int64) if !ok { return fmt.Errorf("unexpected type %T for field %s", value, name) } diff --git a/ent/runtime.go b/ent/runtime.go index bf839d1..770c5fa 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -18,11 +18,11 @@ func init() { documentFields := schema.Document{}.Fields() _ = documentFields // documentDescCreatedAt is the schema descriptor for created_at field. - documentDescCreatedAt := documentFields[2].Descriptor() + documentDescCreatedAt := documentFields[3].Descriptor() // document.DefaultCreatedAt holds the default value on creation for the created_at field. document.DefaultCreatedAt = documentDescCreatedAt.Default.(func() time.Time) // documentDescUpdatedAt is the schema descriptor for updated_at field. - documentDescUpdatedAt := documentFields[3].Descriptor() + documentDescUpdatedAt := documentFields[4].Descriptor() // document.DefaultUpdatedAt holds the default value on creation for the updated_at field. document.DefaultUpdatedAt = documentDescUpdatedAt.Default.(func() time.Time) documentblockFields := schema.DocumentBlock{}.Fields() diff --git a/ent/schema/document.go b/ent/schema/document.go index 17844d7..60fe1ae 100644 --- a/ent/schema/document.go +++ b/ent/schema/document.go @@ -17,6 +17,7 @@ func (Document) Fields() []ent.Field { return []ent.Field{ field.String("name"), field.String("user_id"), + field.Int64("library_id"), field.Time("created_at").Default(time.Now), field.Time("updated_at").Default(time.Now), diff --git a/ent/schema/documentblock.go b/ent/schema/documentblock.go index 4b0f57a..8577bd6 100644 --- a/ent/schema/documentblock.go +++ b/ent/schema/documentblock.go @@ -19,9 +19,9 @@ func (DocumentBlock) Fields() []ent.Field { field.String("name"), field.String("content"), // order - field.Int("order"), + field.Int64("order"), // document id - field.Int("document_id"), + field.Int64("document_id"), field.String("user_id"), field.Time("created_at").Default(time.Now), diff --git a/internal/handlers/controllers/library/library.go b/internal/handlers/controllers/library/library.go index 055bd64..93cda5b 100644 --- a/internal/handlers/controllers/library/library.go +++ b/internal/handlers/controllers/library/library.go @@ -22,16 +22,16 @@ var libraryLogic = logic.NewLibraryLogic() // @Summary 获取当前用户的资料库 // @Success 200 {array} models.ListLibraryResponse // @Router /library [get] -func (c *Controller) Library(ctx *gin.Context) { - libraries, _ := libraryLogic.ListLibrary(ctx) - - libraryResponse := models.ListLibraryResponse{ - Libraries: libraries, - } - - //helper.Response(ctx, http.StatusOK, libraryResponse) - ctx.JSON(http.StatusOK, libraryResponse) -} +//func (c *Controller) Library(ctx *gin.Context) { +// libraries, _ := libraryLogic.ListLibrary(ctx) +// +// libraryResponse := models.ListLibraryResponse{ +// Libraries: libraries, +// } +// +// //helper.Response(ctx, http.StatusOK, libraryResponse) +// ctx.JSON(http.StatusOK, libraryResponse) +//} // CreateLibrary 创建一个资料库 godoc // @Summary 创建一个资料库 diff --git a/internal/logic/document.go b/internal/logic/document.go index 4559beb..dc955de 100644 --- a/internal/logic/document.go +++ b/internal/logic/document.go @@ -2,24 +2,56 @@ package logic import ( "context" + "errors" "leafdev.top/leaf/rag/ent" "leafdev.top/leaf/rag/ent/document" ) type DocumentLogic struct { + LibraryLogic LibraryLogic } func NewDocumentLogic() *DocumentLogic { - return &DocumentLogic{} + return &DocumentLogic{ + LibraryLogic: *NewLibraryLogic(), + } } -func (DocumentLogic) ListDocument(ctx context.Context) ([]*ent.Document, error) { - return orm.Document.Query().Where(document.UserIDEQ(GetUserId(ctx))).All(ctx) +func (d DocumentLogic) ListDocument(ctx context.Context, libraryId int64) ([]*ent.Document, error) { + library, err := d.LibraryLogic.ExistsByUser(ctx, int(libraryId)) + + if err != nil { + return nil, err + } + + if !library { + return nil, errors.New("我们在你的账户下找不到这个资料库") + } + + return orm.Document.Query().Where( + document.UserIDEQ(GetUserId(ctx)), + document.LibraryIDEQ(libraryId), + ).All(ctx) } -func (DocumentLogic) CreateDocument(ctx context.Context, name string) (*ent.Document, error) { +func (d DocumentLogic) CreateDocument(ctx context.Context, libraryId int64, name string) (*ent.Document, error) { + // 检查 Library 是否存在 + exists, err := d.LibraryLogic.ExistsByUser(ctx, int(libraryId)) + if err != nil { + return nil, err + } + + if !exists { + return nil, errors.New("library not exists") + } + + if name == "" { + return nil, errors.New("name cannot be empty") + } + return orm.Document.Create(). SetName(name). SetUserID(GetUserId(ctx)). + SetLibraryID(libraryId). Save(ctx) } diff --git a/internal/logic/library.go b/internal/logic/library.go index dd82153..c784e0d 100644 --- a/internal/logic/library.go +++ b/internal/logic/library.go @@ -5,17 +5,23 @@ import ( "errors" "leafdev.top/leaf/rag/ent" "leafdev.top/leaf/rag/ent/library" + "leafdev.top/leaf/rag/internal/providers/helper" ) type LibraryLogic struct { } +var pageSize = 10 + func NewLibraryLogic() *LibraryLogic { return &LibraryLogic{} } -func (l *LibraryLogic) ListLibrary(ctx context.Context) ([]*ent.Library, error) { - return orm.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx) +func (l *LibraryLogic) ListLibrary(ctx context.Context, page int) ([]*ent.Library, error) { + return orm.Library.Query().Where(library.UserID(GetUserId(ctx))). + Offset(helper.Offset(page, pageSize)). + Limit(pageSize). + All(ctx) } func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Library, error) { @@ -29,3 +35,25 @@ func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Lib SetUserID(GetUserId(ctx)). Save(ctx) } + +func (l *LibraryLogic) DeleteLibrary(ctx context.Context, id int) error { + return orm.Library.DeleteOneID(id).Exec(ctx) +} + +func (l *LibraryLogic) UpdateLibrary(ctx context.Context, id int, name string) (*ent.Library, error) { + return orm.Library.UpdateOneID(id). + SetName(name). + Save(ctx) +} + +func (l *LibraryLogic) GetLibrary(ctx context.Context, id int) (*ent.Library, error) { + return orm.Library.Get(ctx, id) +} + +func (l *LibraryLogic) Exists(ctx context.Context, id int) (bool, error) { + return orm.Library.Query().Where(library.ID(id)).Exist(ctx) +} + +func (l *LibraryLogic) ExistsByUser(ctx context.Context, libraryId int) (bool, error) { + return orm.Library.Query().Where(library.IDEQ(libraryId), library.UserIDEQ(GetUserId(ctx))).Exist(ctx) +} diff --git a/internal/migrations/20240714155720_create_library.sql b/internal/migrations/20240714155720_create_library.sql deleted file mode 100644 index 2b9ddb9..0000000 --- a/internal/migrations/20240714155720_create_library.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up --- create "libraries" table -CREATE TABLE "libraries" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "user_id" bigint NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, PRIMARY KEY ("id")); --- create index "library_name_user_id" to table: "libraries" -CREATE INDEX "library_name_user_id" ON "libraries" ("name", "user_id"); - --- +goose Down --- reverse: create index "library_name_user_id" to table: "libraries" -DROP INDEX "library_name_user_id"; --- reverse: create "libraries" table -DROP TABLE "libraries"; diff --git a/internal/migrations/20240714164619_update_user_id.sql b/internal/migrations/20240714164619_update_user_id.sql deleted file mode 100644 index 222255b..0000000 --- a/internal/migrations/20240714164619_update_user_id.sql +++ /dev/null @@ -1,7 +0,0 @@ --- +goose Up --- modify "libraries" table -ALTER TABLE "libraries" ALTER COLUMN "user_id" TYPE character varying; - --- +goose Down --- reverse: modify "libraries" table -ALTER TABLE "libraries" ALTER COLUMN "user_id" TYPE bigint; diff --git a/internal/migrations/20240720092815_add_documents_tables.sql b/internal/migrations/20240720092815_add_documents_tables.sql deleted file mode 100644 index ea4ee6a..0000000 --- a/internal/migrations/20240720092815_add_documents_tables.sql +++ /dev/null @@ -1,19 +0,0 @@ --- +goose Up --- create "document_blocks" table -CREATE TABLE "document_blocks" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "content" character varying NOT NULL, "document_id" bigint NOT NULL, "user_id" character varying NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, PRIMARY KEY ("id")); --- create index "documentblock_name_user_id" to table: "document_blocks" -CREATE INDEX "documentblock_name_user_id" ON "document_blocks" ("name", "user_id"); --- create "documents" table -CREATE TABLE "documents" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "user_id" character varying NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, "document_block_document" bigint NULL, PRIMARY KEY ("id"), CONSTRAINT "documents_document_blocks_document" FOREIGN KEY ("document_block_document") REFERENCES "document_blocks" ("id") ON DELETE SET NULL); --- create index "document_name_user_id" to table: "documents" -CREATE INDEX "document_name_user_id" ON "documents" ("name", "user_id"); - --- +goose Down --- reverse: create index "document_name_user_id" to table: "documents" -DROP INDEX "document_name_user_id"; --- reverse: create "documents" table -DROP TABLE "documents"; --- reverse: create index "documentblock_name_user_id" to table: "document_blocks" -DROP INDEX "documentblock_name_user_id"; --- reverse: create "document_blocks" table -DROP TABLE "document_blocks"; diff --git a/internal/migrations/20240720093157_add_order_to_document_blocks.sql b/internal/migrations/20240720093157_add_order_to_document_blocks.sql deleted file mode 100644 index 12f24b4..0000000 --- a/internal/migrations/20240720093157_add_order_to_document_blocks.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up --- modify "document_blocks" table -ALTER TABLE "document_blocks" ADD COLUMN "order" bigint NOT NULL; --- create index "documentblock_name_user_id_order" to table: "document_blocks" -CREATE INDEX "documentblock_name_user_id_order" ON "document_blocks" ("name", "user_id", "order"); - --- +goose Down --- reverse: create index "documentblock_name_user_id_order" to table: "document_blocks" -DROP INDEX "documentblock_name_user_id_order"; --- reverse: modify "document_blocks" table -ALTER TABLE "document_blocks" DROP COLUMN "order"; diff --git a/internal/migrations/20240720155522_init.sql b/internal/migrations/20240720155522_init.sql new file mode 100644 index 0000000..67e930c --- /dev/null +++ b/internal/migrations/20240720155522_init.sql @@ -0,0 +1,27 @@ +-- +goose Up +-- create "document_blocks" table +CREATE TABLE "document_blocks" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "content" character varying NOT NULL, "order" bigint NOT NULL, "document_id" bigint NOT NULL, "user_id" character varying NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, PRIMARY KEY ("id")); +-- create index "documentblock_name_user_id_order" to table: "document_blocks" +CREATE INDEX "documentblock_name_user_id_order" ON "document_blocks" ("name", "user_id", "order"); +-- create "libraries" table +CREATE TABLE "libraries" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "user_id" character varying NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, PRIMARY KEY ("id")); +-- create index "library_name_user_id" to table: "libraries" +CREATE INDEX "library_name_user_id" ON "libraries" ("name", "user_id"); +-- create "documents" table +CREATE TABLE "documents" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "name" character varying NOT NULL, "user_id" character varying NOT NULL, "library_id" bigint NOT NULL, "created_at" timestamptz NOT NULL, "updated_at" timestamptz NOT NULL, "document_block_document" bigint NULL, PRIMARY KEY ("id"), CONSTRAINT "documents_document_blocks_document" FOREIGN KEY ("document_block_document") REFERENCES "document_blocks" ("id") ON DELETE SET NULL); +-- create index "document_name_user_id" to table: "documents" +CREATE INDEX "document_name_user_id" ON "documents" ("name", "user_id"); + +-- +goose Down +-- reverse: create index "document_name_user_id" to table: "documents" +DROP INDEX "document_name_user_id"; +-- reverse: create "documents" table +DROP TABLE "documents"; +-- reverse: create index "library_name_user_id" to table: "libraries" +DROP INDEX "library_name_user_id"; +-- reverse: create "libraries" table +DROP TABLE "libraries"; +-- reverse: create index "documentblock_name_user_id_order" to table: "document_blocks" +DROP INDEX "documentblock_name_user_id_order"; +-- reverse: create "document_blocks" table +DROP TABLE "document_blocks"; diff --git a/internal/migrations/atlas.sum b/internal/migrations/atlas.sum index 3410112..2de6599 100644 --- a/internal/migrations/atlas.sum +++ b/internal/migrations/atlas.sum @@ -1,5 +1,2 @@ -h1:sLEQ6Lxd6zOalx8i43YLFGrjyKJrnXXnj8KDE/eqF3A= -20240714155720_create_library.sql h1:nfzvzMxwqJRhmvUQSnXcK/LZDyAfy/vnjx7ZD2vY3AU= -20240714164619_update_user_id.sql h1:AUCFOY9OVkR4W/sFc7Ipb5Cz3xR4gY61jDuTN4MN7w8= -20240720092815_add_documents_tables.sql h1:S0OeUzCLWqXtA1Wi2Wvmlj/2eg2Kl0NkPNRy7Mu+2MY= -20240720093157_add_order_to_document_blocks.sql h1:6CHgxARw5yXmXfHTzFFPxqZZ8bWl3g76KHXlB3XYeLQ= +h1:hDIlYDlNwP2WQt4WZ9vaeH5D8IAoeLPMBHidHnSKaIs= +20240720155522_init.sql h1:CKqDLw2sVT+NMqnmrvwtkmbHwNFJTUbdR5q0OtoxF1k= diff --git a/internal/routes/router.go b/internal/routes/router.go index e5241d0..bb6bd57 100644 --- a/internal/routes/router.go +++ b/internal/routes/router.go @@ -45,7 +45,7 @@ func InitApiRoutes() { authorizedApiV1.Use(http.RequireJWTIDToken) { authorizedApiV1.GET("/user", userController.CurrentUser) - authorizedApiV1.GET("/library", libraryController.Library) + //authorizedApiV1.GET("/library", libraryController.Library) authorizedApiV1.POST("/library", libraryController.CreateLibrary) } diff --git a/internal/services/rag/documentService.go b/internal/services/rag/documentService.go index 003f213..1c29fdf 100644 --- a/internal/services/rag/documentService.go +++ b/internal/services/rag/documentService.go @@ -7,7 +7,7 @@ import ( ) func (Service) ListDocument(ctx context.Context, req *ragApi.ListDocumentRequest) (*ragApi.ListDocumentResponse, error) { - document, err := documentLogic.ListDocument(ctx) + document, err := documentLogic.ListDocument(ctx, req.LibraryId) if err != nil { return nil, err } @@ -18,6 +18,7 @@ func (Service) ListDocument(ctx context.Context, req *ragApi.ListDocumentRequest Id: int64(document.ID), Name: document.Name, UserId: document.UserID, + LibraryId: document.LibraryID, CreatedAt: document.CreatedAt.String(), UpdatedAt: document.UpdatedAt.String(), } @@ -28,7 +29,7 @@ func (Service) ListDocument(ctx context.Context, req *ragApi.ListDocumentRequest } func (Service) CreateDocument(ctx context.Context, req *ragApi.CreateDocumentRequest) (*ragApi.CreateDocumentResponse, error) { - document, err := documentLogic.CreateDocument(ctx, req.Name) + document, err := documentLogic.CreateDocument(ctx, req.LibraryId, req.Name) if err != nil { return nil, err } diff --git a/internal/services/rag/libraryservice.go b/internal/services/rag/libraryservice.go index 6760918..43520fd 100644 --- a/internal/services/rag/libraryservice.go +++ b/internal/services/rag/libraryservice.go @@ -2,12 +2,11 @@ package rag import ( "context" - "github.com/mitchellh/mapstructure" ragApi "leafdev.top/leaf/rag/api/rag" ) -func (Service) ListLibrary(ctx context.Context, _ *ragApi.ListLibraryRequest) (*ragApi.ListLibraryResponse, error) { - libraries, err := libraryLogic.ListLibrary(ctx) +func (Service) ListLibrary(ctx context.Context, req *ragApi.ListLibraryRequest) (*ragApi.ListLibraryResponse, error) { + libraries, err := libraryLogic.ListLibrary(ctx, int(req.Page)) if err != nil { return nil, err } @@ -35,10 +34,14 @@ func (Service) CreateLibrary(ctx context.Context, req *ragApi.CreateLibraryReque return nil, err } - var libraryApiResponse ragApi.CreateLibraryResponse - err = mapstructure.Decode(library, &libraryApiResponse) - if err != nil { - return nil, err + var libraryApiResponse = ragApi.CreateLibraryResponse{ + Library: &ragApi.Library{ + Id: int64(library.ID), + Name: library.Name, + UserId: library.UserID, + CreatedAt: library.CreatedAt.String(), + UpdatedAt: library.UpdatedAt.String(), + }, } return &libraryApiResponse, nil