改进 swag doc

增加 创建和列出 library
This commit is contained in:
Twilight 2024-07-15 01:22:40 +08:00
parent 0d613d24dd
commit df929aa104
31 changed files with 436 additions and 146 deletions

View File

@ -8,5 +8,8 @@ grpc-web:
grpcwebproxy --backend_addr=localhost:8081 --run_http_server --run_tls_server=false --server_http_debug_port 18081 --allow_all_origins --server_bind_address 127.0.0.1
install-deps:
go install github.com/swaggo/swag/cmd/swag@latest
curl -sSf https://atlasgo.sh | sh
swag:
swag init -g internal/routes/router.go --parseDependency
swag init -g internal/routes/router.go --parseDependency
hash-migration:
atlas migrate hash --dir "file://internal/migrations"

View File

@ -24,24 +24,86 @@ const docTemplate = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/": {
"/library": {
"get": {
"summary": "获取当前用户的请求",
"summary": "获取当前用户的资料库",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.CurrentUserResponse"
"$ref": "#/definitions/models.ListLibraryResponse"
}
}
}
}
},
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"summary": "创建一个资料库",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.CreateLibraryResponse"
}
}
}
}
},
"/user": {
"get": {
"summary": "获取当前用户的请求",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.CurrentUserResponse"
}
}
}
}
}
},
"definitions": {
"ent.Library": {
"type": "object",
"properties": {
"created_at": {
"description": "CreatedAt holds the value of the \"created_at\" field.",
"type": "string"
},
"id": {
"description": "ID of the ent.",
"type": "integer"
},
"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"
},
"user_id": {
"description": "UserID holds the value of the \"user_id\" field.",
"type": "string"
}
}
},
"models.CreateLibraryResponse": {
"type": "object",
"properties": {
"library": {
"$ref": "#/definitions/ent.Library"
}
}
},
"models.CurrentUserResponse": {
"type": "object",
"properties": {
@ -58,6 +120,17 @@ const docTemplate = `{
"type": "boolean"
}
}
},
"models.ListLibraryResponse": {
"type": "object",
"properties": {
"libraries": {
"type": "array",
"items": {
"$ref": "#/definitions/ent.Library"
}
}
}
}
},
"securityDefinitions": {
@ -74,7 +147,7 @@ const docTemplate = `{
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "localhost:8080",
BasePath: "/",
BasePath: "/api/v1",
Schemes: []string{},
Title: "Swagger Example API",
Description: "This is a sample server celler server.",

View File

@ -16,26 +16,88 @@
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/",
"basePath": "/api/v1",
"paths": {
"/": {
"/library": {
"get": {
"summary": "获取当前用户的请求",
"summary": "获取当前用户的资料库",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/models.CurrentUserResponse"
"$ref": "#/definitions/models.ListLibraryResponse"
}
}
}
}
},
"post": {
"security": [
{
"ApiKeyAuth": []
}
],
"summary": "创建一个资料库",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.CreateLibraryResponse"
}
}
}
}
},
"/user": {
"get": {
"summary": "获取当前用户的请求",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.CurrentUserResponse"
}
}
}
}
}
},
"definitions": {
"ent.Library": {
"type": "object",
"properties": {
"created_at": {
"description": "CreatedAt holds the value of the \"created_at\" field.",
"type": "string"
},
"id": {
"description": "ID of the ent.",
"type": "integer"
},
"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"
},
"user_id": {
"description": "UserID holds the value of the \"user_id\" field.",
"type": "string"
}
}
},
"models.CreateLibraryResponse": {
"type": "object",
"properties": {
"library": {
"$ref": "#/definitions/ent.Library"
}
}
},
"models.CurrentUserResponse": {
"type": "object",
"properties": {
@ -52,6 +114,17 @@
"type": "boolean"
}
}
},
"models.ListLibraryResponse": {
"type": "object",
"properties": {
"libraries": {
"type": "array",
"items": {
"$ref": "#/definitions/ent.Library"
}
}
}
}
},
"securityDefinitions": {

View File

@ -1,5 +1,28 @@
basePath: /
basePath: /api/v1
definitions:
ent.Library:
properties:
created_at:
description: CreatedAt holds the value of the "created_at" field.
type: string
id:
description: ID of the ent.
type: integer
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
user_id:
description: UserID holds the value of the "user_id" field.
type: string
type: object
models.CreateLibraryResponse:
properties:
library:
$ref: '#/definitions/ent.Library'
type: object
models.CurrentUserResponse:
properties:
ip:
@ -11,6 +34,13 @@ definitions:
valid:
type: boolean
type: object
models.ListLibraryResponse:
properties:
libraries:
items:
$ref: '#/definitions/ent.Library'
type: array
type: object
host: localhost:8080
info:
contact:
@ -25,15 +55,32 @@ info:
title: Swagger Example API
version: "1.0"
paths:
/:
/library:
get:
responses:
"200":
description: OK
schema:
items:
$ref: '#/definitions/models.CurrentUserResponse'
$ref: '#/definitions/models.ListLibraryResponse'
type: array
summary: 获取当前用户的资料库
post:
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.CreateLibraryResponse'
security:
- ApiKeyAuth: []
summary: 创建一个资料库
/user:
get:
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.CurrentUserResponse'
summary: 获取当前用户的请求
securityDefinitions:
ApiKeyAuth:

View File

@ -11,11 +11,10 @@ import (
"leafdev.top/leaf/rag/ent/migrate"
"leafdev.top/leaf/rag/ent/library"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql"
"leafdev.top/leaf/rag/ent/library"
)
// Client is the client that holds all ent builders.

View File

@ -6,13 +6,13 @@ import (
"context"
"errors"
"fmt"
"leafdev.top/leaf/rag/ent/library"
"reflect"
"sync"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"leafdev.top/leaf/rag/ent/library"
)
// ent aliases to avoid import conflicts in user's code.

View File

@ -4,13 +4,13 @@ package enttest
import (
"context"
"leafdev.top/leaf/rag/ent"
// required by schema hooks.
_ "leafdev.top/leaf/rag/ent/runtime"
"leafdev.top/leaf/rag/ent/migrate"
"entgo.io/ent/dialect/sql/schema"
"leafdev.top/leaf/rag/ent/migrate"
)
type (

View File

@ -5,6 +5,7 @@ package hook
import (
"context"
"fmt"
"leafdev.top/leaf/rag/ent"
)

View File

@ -4,12 +4,12 @@ package ent
import (
"fmt"
"leafdev.top/leaf/rag/ent/library"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"leafdev.top/leaf/rag/ent/library"
)
// Library is the model entity for the Library schema.
@ -20,7 +20,7 @@ type Library struct {
// Name holds the value of the "name" field.
Name string `json:"name,omitempty"`
// UserID holds the value of the "user_id" field.
UserID uint64 `json:"user_id,omitempty"`
UserID string `json:"user_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.
@ -33,9 +33,9 @@ func (*Library) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case library.FieldID, library.FieldUserID:
case library.FieldID:
values[i] = new(sql.NullInt64)
case library.FieldName:
case library.FieldName, library.FieldUserID:
values[i] = new(sql.NullString)
case library.FieldCreatedAt, library.FieldUpdatedAt:
values[i] = new(sql.NullTime)
@ -67,10 +67,10 @@ func (l *Library) assignValues(columns []string, values []any) error {
l.Name = value.String
}
case library.FieldUserID:
if value, ok := values[i].(*sql.NullInt64); !ok {
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field user_id", values[i])
} else if value.Valid {
l.UserID = uint64(value.Int64)
l.UserID = value.String
}
case library.FieldCreatedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
@ -124,7 +124,7 @@ func (l *Library) String() string {
builder.WriteString(l.Name)
builder.WriteString(", ")
builder.WriteString("user_id=")
builder.WriteString(fmt.Sprintf("%v", l.UserID))
builder.WriteString(l.UserID)
builder.WriteString(", ")
builder.WriteString("created_at=")
builder.WriteString(l.CreatedAt.Format(time.ANSIC))

View File

@ -3,10 +3,10 @@
package library
import (
"leafdev.top/leaf/rag/ent/predicate"
"time"
"entgo.io/ent/dialect/sql"
"leafdev.top/leaf/rag/ent/predicate"
)
// ID filters vertices based on their ID field.
@ -60,7 +60,7 @@ func Name(v string) predicate.Library {
}
// UserID applies equality check predicate on the "user_id" field. It's identical to UserIDEQ.
func UserID(v uint64) predicate.Library {
func UserID(v string) predicate.Library {
return predicate.Library(sql.FieldEQ(FieldUserID, v))
}
@ -140,45 +140,70 @@ func NameContainsFold(v string) predicate.Library {
}
// UserIDEQ applies the EQ predicate on the "user_id" field.
func UserIDEQ(v uint64) predicate.Library {
func UserIDEQ(v string) predicate.Library {
return predicate.Library(sql.FieldEQ(FieldUserID, v))
}
// UserIDNEQ applies the NEQ predicate on the "user_id" field.
func UserIDNEQ(v uint64) predicate.Library {
func UserIDNEQ(v string) predicate.Library {
return predicate.Library(sql.FieldNEQ(FieldUserID, v))
}
// UserIDIn applies the In predicate on the "user_id" field.
func UserIDIn(vs ...uint64) predicate.Library {
func UserIDIn(vs ...string) predicate.Library {
return predicate.Library(sql.FieldIn(FieldUserID, vs...))
}
// UserIDNotIn applies the NotIn predicate on the "user_id" field.
func UserIDNotIn(vs ...uint64) predicate.Library {
func UserIDNotIn(vs ...string) predicate.Library {
return predicate.Library(sql.FieldNotIn(FieldUserID, vs...))
}
// UserIDGT applies the GT predicate on the "user_id" field.
func UserIDGT(v uint64) predicate.Library {
func UserIDGT(v string) predicate.Library {
return predicate.Library(sql.FieldGT(FieldUserID, v))
}
// UserIDGTE applies the GTE predicate on the "user_id" field.
func UserIDGTE(v uint64) predicate.Library {
func UserIDGTE(v string) predicate.Library {
return predicate.Library(sql.FieldGTE(FieldUserID, v))
}
// UserIDLT applies the LT predicate on the "user_id" field.
func UserIDLT(v uint64) predicate.Library {
func UserIDLT(v string) predicate.Library {
return predicate.Library(sql.FieldLT(FieldUserID, v))
}
// UserIDLTE applies the LTE predicate on the "user_id" field.
func UserIDLTE(v uint64) predicate.Library {
func UserIDLTE(v string) predicate.Library {
return predicate.Library(sql.FieldLTE(FieldUserID, v))
}
// UserIDContains applies the Contains predicate on the "user_id" field.
func UserIDContains(v string) predicate.Library {
return predicate.Library(sql.FieldContains(FieldUserID, v))
}
// UserIDHasPrefix applies the HasPrefix predicate on the "user_id" field.
func UserIDHasPrefix(v string) predicate.Library {
return predicate.Library(sql.FieldHasPrefix(FieldUserID, v))
}
// UserIDHasSuffix applies the HasSuffix predicate on the "user_id" field.
func UserIDHasSuffix(v string) predicate.Library {
return predicate.Library(sql.FieldHasSuffix(FieldUserID, v))
}
// UserIDEqualFold applies the EqualFold predicate on the "user_id" field.
func UserIDEqualFold(v string) predicate.Library {
return predicate.Library(sql.FieldEqualFold(FieldUserID, v))
}
// UserIDContainsFold applies the ContainsFold predicate on the "user_id" field.
func UserIDContainsFold(v string) predicate.Library {
return predicate.Library(sql.FieldContainsFold(FieldUserID, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.Library {
return predicate.Library(sql.FieldEQ(FieldCreatedAt, v))

View File

@ -6,11 +6,11 @@ import (
"context"
"errors"
"fmt"
"leafdev.top/leaf/rag/ent/library"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"leafdev.top/leaf/rag/ent/library"
)
// LibraryCreate is the builder for creating a Library entity.
@ -27,8 +27,8 @@ func (lc *LibraryCreate) SetName(s string) *LibraryCreate {
}
// SetUserID sets the "user_id" field.
func (lc *LibraryCreate) SetUserID(u uint64) *LibraryCreate {
lc.mutation.SetUserID(u)
func (lc *LibraryCreate) SetUserID(s string) *LibraryCreate {
lc.mutation.SetUserID(s)
return lc
}
@ -150,7 +150,7 @@ func (lc *LibraryCreate) createSpec() (*Library, *sqlgraph.CreateSpec) {
_node.Name = value
}
if value, ok := lc.mutation.UserID(); ok {
_spec.SetField(library.FieldUserID, field.TypeUint64, value)
_spec.SetField(library.FieldUserID, field.TypeString, value)
_node.UserID = value
}
if value, ok := lc.mutation.CreatedAt(); ok {

View File

@ -4,12 +4,12 @@ package ent
import (
"context"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
)
// LibraryDelete is the builder for deleting a Library entity.

View File

@ -5,13 +5,13 @@ package ent
import (
"context"
"fmt"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
"math"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
)
// LibraryQuery is the builder for querying Library entities.

View File

@ -6,13 +6,13 @@ import (
"context"
"errors"
"fmt"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
)
// LibraryUpdate is the builder for updating Library entities.
@ -43,26 +43,19 @@ func (lu *LibraryUpdate) SetNillableName(s *string) *LibraryUpdate {
}
// SetUserID sets the "user_id" field.
func (lu *LibraryUpdate) SetUserID(u uint64) *LibraryUpdate {
lu.mutation.ResetUserID()
lu.mutation.SetUserID(u)
func (lu *LibraryUpdate) SetUserID(s string) *LibraryUpdate {
lu.mutation.SetUserID(s)
return lu
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (lu *LibraryUpdate) SetNillableUserID(u *uint64) *LibraryUpdate {
if u != nil {
lu.SetUserID(*u)
func (lu *LibraryUpdate) SetNillableUserID(s *string) *LibraryUpdate {
if s != nil {
lu.SetUserID(*s)
}
return lu
}
// AddUserID adds u to the "user_id" field.
func (lu *LibraryUpdate) AddUserID(u int64) *LibraryUpdate {
lu.mutation.AddUserID(u)
return lu
}
// SetCreatedAt sets the "created_at" field.
func (lu *LibraryUpdate) SetCreatedAt(t time.Time) *LibraryUpdate {
lu.mutation.SetCreatedAt(t)
@ -136,10 +129,7 @@ func (lu *LibraryUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec.SetField(library.FieldName, field.TypeString, value)
}
if value, ok := lu.mutation.UserID(); ok {
_spec.SetField(library.FieldUserID, field.TypeUint64, value)
}
if value, ok := lu.mutation.AddedUserID(); ok {
_spec.AddField(library.FieldUserID, field.TypeUint64, value)
_spec.SetField(library.FieldUserID, field.TypeString, value)
}
if value, ok := lu.mutation.CreatedAt(); ok {
_spec.SetField(library.FieldCreatedAt, field.TypeTime, value)
@ -182,26 +172,19 @@ func (luo *LibraryUpdateOne) SetNillableName(s *string) *LibraryUpdateOne {
}
// SetUserID sets the "user_id" field.
func (luo *LibraryUpdateOne) SetUserID(u uint64) *LibraryUpdateOne {
luo.mutation.ResetUserID()
luo.mutation.SetUserID(u)
func (luo *LibraryUpdateOne) SetUserID(s string) *LibraryUpdateOne {
luo.mutation.SetUserID(s)
return luo
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (luo *LibraryUpdateOne) SetNillableUserID(u *uint64) *LibraryUpdateOne {
if u != nil {
luo.SetUserID(*u)
func (luo *LibraryUpdateOne) SetNillableUserID(s *string) *LibraryUpdateOne {
if s != nil {
luo.SetUserID(*s)
}
return luo
}
// AddUserID adds u to the "user_id" field.
func (luo *LibraryUpdateOne) AddUserID(u int64) *LibraryUpdateOne {
luo.mutation.AddUserID(u)
return luo
}
// SetCreatedAt sets the "created_at" field.
func (luo *LibraryUpdateOne) SetCreatedAt(t time.Time) *LibraryUpdateOne {
luo.mutation.SetCreatedAt(t)
@ -305,10 +288,7 @@ func (luo *LibraryUpdateOne) sqlSave(ctx context.Context) (_node *Library, err e
_spec.SetField(library.FieldName, field.TypeString, value)
}
if value, ok := luo.mutation.UserID(); ok {
_spec.SetField(library.FieldUserID, field.TypeUint64, value)
}
if value, ok := luo.mutation.AddedUserID(); ok {
_spec.AddField(library.FieldUserID, field.TypeUint64, value)
_spec.SetField(library.FieldUserID, field.TypeString, value)
}
if value, ok := luo.mutation.CreatedAt(); ok {
_spec.SetField(library.FieldCreatedAt, field.TypeTime, value)

View File

@ -12,7 +12,7 @@ var (
LibrariesColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "name", Type: field.TypeString},
{Name: "user_id", Type: field.TypeUint64},
{Name: "user_id", Type: field.TypeString},
{Name: "created_at", Type: field.TypeTime},
{Name: "updated_at", Type: field.TypeTime},
}

View File

@ -6,13 +6,13 @@ import (
"context"
"errors"
"fmt"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
"sync"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/predicate"
)
const (
@ -34,8 +34,7 @@ type LibraryMutation struct {
typ string
id *int
name *string
user_id *uint64
adduser_id *int64
user_id *string
created_at *time.Time
updated_at *time.Time
clearedFields map[string]struct{}
@ -179,13 +178,12 @@ func (m *LibraryMutation) ResetName() {
}
// SetUserID sets the "user_id" field.
func (m *LibraryMutation) SetUserID(u uint64) {
m.user_id = &u
m.adduser_id = nil
func (m *LibraryMutation) SetUserID(s string) {
m.user_id = &s
}
// UserID returns the value of the "user_id" field in the mutation.
func (m *LibraryMutation) UserID() (r uint64, exists bool) {
func (m *LibraryMutation) UserID() (r string, exists bool) {
v := m.user_id
if v == nil {
return
@ -196,7 +194,7 @@ func (m *LibraryMutation) UserID() (r uint64, exists bool) {
// OldUserID returns the old "user_id" field's value of the Library entity.
// If the Library 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 *LibraryMutation) OldUserID(ctx context.Context) (v uint64, err error) {
func (m *LibraryMutation) OldUserID(ctx context.Context) (v string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldUserID is only allowed on UpdateOne operations")
}
@ -210,28 +208,9 @@ func (m *LibraryMutation) OldUserID(ctx context.Context) (v uint64, err error) {
return oldValue.UserID, nil
}
// AddUserID adds u to the "user_id" field.
func (m *LibraryMutation) AddUserID(u int64) {
if m.adduser_id != nil {
*m.adduser_id += u
} else {
m.adduser_id = &u
}
}
// AddedUserID returns the value that was added to the "user_id" field in this mutation.
func (m *LibraryMutation) AddedUserID() (r int64, exists bool) {
v := m.adduser_id
if v == nil {
return
}
return *v, true
}
// ResetUserID resets all changes to the "user_id" field.
func (m *LibraryMutation) ResetUserID() {
m.user_id = nil
m.adduser_id = nil
}
// SetCreatedAt sets the "created_at" field.
@ -403,7 +382,7 @@ func (m *LibraryMutation) SetField(name string, value ent.Value) error {
m.SetName(v)
return nil
case library.FieldUserID:
v, ok := value.(uint64)
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
@ -430,21 +409,13 @@ func (m *LibraryMutation) SetField(name string, value ent.Value) error {
// AddedFields returns all numeric fields that were incremented/decremented during
// this mutation.
func (m *LibraryMutation) AddedFields() []string {
var fields []string
if m.adduser_id != nil {
fields = append(fields, library.FieldUserID)
}
return fields
return nil
}
// 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 *LibraryMutation) AddedField(name string) (ent.Value, bool) {
switch name {
case library.FieldUserID:
return m.AddedUserID()
}
return nil, false
}
@ -453,13 +424,6 @@ func (m *LibraryMutation) AddedField(name string) (ent.Value, bool) {
// type.
func (m *LibraryMutation) AddField(name string, value ent.Value) error {
switch name {
case library.FieldUserID:
v, ok := value.(int64)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddUserID(v)
return nil
}
return fmt.Errorf("unknown Library numeric field %s", name)
}

View File

@ -3,9 +3,10 @@
package ent
import (
"time"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/ent/schema"
"time"
)
// The init function reads all schema descriptors with runtime code

View File

@ -2,7 +2,7 @@
package runtime
// The schema-stitching logic is generated in framework_v2/ent/runtime.go
// The schema-stitching logic is generated in leafdev.top/leaf/rag/ent/runtime.go
const (
Version = "v0.13.1" // Version of ent codegen.

View File

@ -16,7 +16,7 @@ type Library struct {
func (Library) Fields() []ent.Field {
return []ent.Field{
field.String("name"),
field.Uint64("user_id"),
field.String("user_id"),
field.Time("created_at").Default(time.Now),
field.Time("updated_at").Default(time.Now),
}

4
go.sum
View File

@ -150,6 +150,8 @@ github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
@ -170,6 +172,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@ -1,6 +1,13 @@
package library
import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"leafdev.top/leaf/rag/internal/logic"
"leafdev.top/leaf/rag/internal/providers/helper"
"leafdev.top/leaf/rag/models"
"net/http"
)
type Controller struct {
}
@ -9,11 +16,59 @@ func NewLibraryController() *Controller {
return &Controller{}
}
var libraryLogic = logic.NewLibraryLogic()
// Library 获取当前用户的资料库 godoc
// @Success 200 {array} models.CurrentUserResponse
// @Router / [get]
// @Summary 获取当前用户的资料库
// @Success 200 {array} models.ListLibraryResponse
// @Router /library [get]
func (c *Controller) Library(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "ok",
libraries, _ := libraryLogic.ListLibrary(ctx)
libraryResponse := models.ListLibraryResponse{
Libraries: libraries,
}
//helper.Response(ctx, http.StatusOK, libraryResponse)
ctx.JSON(http.StatusOK, libraryResponse)
}
// CreateLibrary 创建一个资料库 godoc
// @Summary 创建一个资料库
// @Security ApiKeyAuth
// @Success 200 {object} models.CreateLibraryResponse
// @Router /library [post]
func (c *Controller) CreateLibrary(ctx *gin.Context) {
var createLibraryReq models.CreateLibraryRequest
err := ctx.ShouldBindBodyWithJSON(&createLibraryReq)
if err != nil {
helper.ResponseError(ctx, http.StatusBadRequest, err)
return
}
library, err := libraryLogic.CreateLibrary(ctx, createLibraryReq.Name)
if err != nil {
helper.ResponseError(ctx, http.StatusInternalServerError, err)
return
}
//helper.Response(ctx, http.StatusCreated, models.CreateLibraryResponse{
// Library: library,
//})
ctx.JSON(http.StatusOK, models.CreateLibraryResponse{
Library: library,
})
}
func MustBindBodyWithJSON(ctx *gin.Context, obj interface{}) error {
err := ctx.ShouldBindBodyWith(obj, binding.JSON)
if err != nil {
helper.ResponseError(ctx, http.StatusBadRequest, err)
return err
}
return err
}

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"leafdev.top/leaf/rag/internal/logic"
"leafdev.top/leaf/rag/models"
"net/http"
)
var AuthLogic = logic.NewAuthLogic()
@ -16,11 +17,11 @@ func NewUserController() *Controller {
// CurrentUser 获取当前用户 godoc
// @Summary 获取当前用户的请求
// @Success 200 {array} models.CurrentUserResponse
// @Router / [get]
// @Success 200 {object} models.CurrentUserResponse
// @Router /user [get]
func (c *Controller) CurrentUser(ctx *gin.Context) {
var user = AuthLogic.GinUser(ctx)
ctx.JSON(200, models.CurrentUserResponse{
ctx.JSON(http.StatusOK, models.CurrentUserResponse{
IP: ctx.ClientIP(),
Valid: user.Valid,
UserEmail: user.Token.Email,

View File

@ -83,3 +83,8 @@ func (a *AuthLogic) GinUser(c *gin.Context) *models.User {
user, _ := c.Get(consts.AuthMiddlewareKey)
return user.(*models.User)
}
func GetUserId(ctx *gin.Context) string {
logic := AuthLogic{}
return logic.GinUser(ctx).Token.Sub
}

1
internal/logic/base.go Normal file
View File

@ -0,0 +1 @@
package logic

28
internal/logic/library.go Normal file
View File

@ -0,0 +1,28 @@
package logic
import (
"github.com/gin-gonic/gin"
"leafdev.top/leaf/rag/ent"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/internal/providers"
)
type LibraryLogic struct {
}
var client = providers.MustGet[ent.Client]()
func NewLibraryLogic() *LibraryLogic {
return &LibraryLogic{}
}
func (l *LibraryLogic) ListLibrary(ctx *gin.Context) ([]*ent.Library, error) {
return client.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx)
}
func (l *LibraryLogic) CreateLibrary(ctx *gin.Context, name string) (*ent.Library, error) {
return client.Library.Create().
SetName(name).
SetUserID(GetUserId(ctx)).
Save(ctx)
}

View File

@ -0,0 +1,7 @@
-- +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;

View File

@ -1,2 +1,3 @@
h1:UUSMLWHMuU9uMY48Lb2EYjpQSZk/Ngn7OEjDSH7UQlA=
h1:gAvB+J97CWicumiwquwzeenMmWbnkb2SkIa7TZ9+r2w=
20240714155720_create_library.sql h1:JzdeQPcsm2609MV/6DbYu5N7tg583qpxoaXgFtazOzE=
20240714164619_update_user_id.sql h1:YiqNbf8FcKJCH9cj3PIFqOtr/pqzLKJgXza8nw8iO/w=

View File

@ -2,11 +2,11 @@ package helper
import (
"github.com/gin-gonic/gin"
"leafdev.top/leaf/rag/internal/providers/response"
"leafdev.top/leaf/rag/models"
)
func ResponseMessage(c *gin.Context, code int, message string, data interface{}) {
c.JSON(code, &response.BaseResponse{
c.JSON(code, &models.BaseResponse{
Message: message,
Code: code,
Data: data,
@ -15,7 +15,7 @@ func ResponseMessage(c *gin.Context, code int, message string, data interface{})
}
func Response(c *gin.Context, code int, data interface{}) {
c.JSON(code, &response.BaseResponse{
c.JSON(code, &models.BaseResponse{
Code: code,
Data: data,
})
@ -23,7 +23,7 @@ func Response(c *gin.Context, code int, data interface{}) {
}
func ResponseError(c *gin.Context, code int, err error) {
c.JSON(code, &response.BaseResponse{
c.JSON(code, &models.BaseResponse{
Error: err.Error(),
Code: code,
})

View File

@ -5,11 +5,15 @@ import (
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "leafdev.top/leaf/rag/docs"
"leafdev.top/leaf/rag/internal/handlers/controllers/library"
"leafdev.top/leaf/rag/internal/handlers/controllers/user"
"leafdev.top/leaf/rag/internal/middleware/http"
"leafdev.top/leaf/rag/internal/providers"
)
var userController = user.NewUserController()
var libraryController = library.NewLibraryController()
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
@ -23,7 +27,7 @@ import (
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
// @BasePath /api/v1
// @securityDefinitions.apikey ApiKeyAuth
//
@ -34,9 +38,17 @@ import (
func InitApiRoutes() {
var r = *providers.MustGet[gin.Engine]()
var userController = user.NewUserController()
r.Use(http.MiddlewareJSONResponse)
// middleware group
authorizedApiV1 := r.Group("/api/v1")
authorizedApiV1.Use(http.RequireJWTIDToken)
{
authorizedApiV1.GET("/user", userController.CurrentUser)
authorizedApiV1.GET("/library", libraryController.Library)
authorizedApiV1.POST("/library", libraryController.CreateLibrary)
}
r.GET("/", http.MiddlewareJSONResponse, http.RequireJWTIDToken, userController.CurrentUser)
}
// InitSwaggerRoutes init swagger routes

View File

@ -1,4 +1,4 @@
package response
package models
type BaseResponse struct {
Message string `json:"message"`

View File

@ -1,16 +1,26 @@
package models
import "leafdev.top/leaf/rag/ent"
type ListLibraryRequest struct {
}
type ListLibraryResponse struct {
Libraries []LibraryRequest `json:"libraries"`
Libraries []*ent.Library `json:"libraries"`
}
type CreateLibraryRequest struct {
Name string `json:"name" binding:"required"`
}
type CreateLibraryResponse struct {
Library *ent.Library `json:"library"`
}
type Library struct {
LibraryName string `json:"libraryName"`
LibraryID string `json:"libraryId"`
Valid bool `json:"valid"`
Id int `json:"libraryName"`
LibraryID string `json:"libraryId"`
Valid bool `json:"valid"`
}
type LibraryRequest struct {