Compare commits

..

No commits in common. "develop" and "master" have entirely different histories.

46 changed files with 154 additions and 4013 deletions

View File

@ -2,16 +2,9 @@ app:
name: 'share-knowledge-server'
version: '1.0.0'
domain: '127.0.0.1'
is_ssl: false
run_mode: 'debug'
default_context_timeout: 60
page_size: 10
upload_save_path: '/tmp/uploads'
upload_server_path: '/statistic'
upload_avatar_max_size: 5
upload_avatar_allow_exts: ['.jpg','.jpeg','.png']
upload_attachment_max_size: 4096
upload_attachment_allow_exts: ['.doc','.pdf']
server:
http_port: 8001
grpc_port: 8002

View File

@ -7,17 +7,10 @@
package config
type App struct {
Name string `mapstructure:"name" json:"name" yaml:"name"`
Version string `mapstructure:"version" json:"version" yaml:"version"`
Domain string `mapstructure:"domain" json:"domain" yaml:"domain"`
IsSSL bool `mapstructure:"is_ssl" json:"is_ssl" yaml:"is_ssl"`
PageSize int64 `mapstructure:"page_size" json:"page_size" yaml:"page_size"`
RunMode string `mapstructure:"run_mode" json:"run_mode" yaml:"run_mode"`
DefaultContextTimeout int64 `mapstructure:"default_context_timeout" json:"default_context_timeout" yaml:"default_context_timeout"`
UploadSavePath string `mapstructure:"upload_save_path" json:"upload_save_path" yaml:"upload_save_path"`
UploadServerPath string `mapstructure:"upload_server_path" json:"upload_server_path" yaml:"upload_server_path"`
UploadAvatarMaxSize int64 `mapstructure:"upload_avatar_max_size" json:"upload_avatar_max_size" yaml:"upload_avatar_max_size"`
UploadAvatarAllowExts []string `mapstructure:"upload_avatar_allow_exts" json:"upload_avatar_allow_exts" yaml:"upload_avatar_allow_exts"`
UploadAttachmentMaxSize int64 `mapstructure:"upload_attachment_max_size" json:"upload_attachment_max_size" yaml:"upload_attachment_max_size"`
UploadAttachmentAllowExts []string `mapstructure:"upload_attachment_allow_exts" json:"upload_attachment_allow_exts" yaml:"upload_attachment_allow_exts"`
Name string `mapstructure:"name" json:"name" yaml:"name"`
Version string `mapstructure:"version" json:"version" yaml:"version"`
Domain string `mapstructure:"domain" json:"domain" yaml:"domain"`
PageSize int64 `mapstructure:"page_size" json:"page_size" yaml:"page_size"`
RunMode string `mapstructure:"run_mode" json:"run_mode" yaml:"run_mode"`
DefaultContextTimeout int64 `mapstructure:"default_context_timeout" json:"default_context_timeout" yaml:"default_context_timeout"`
}

View File

@ -1,14 +1,22 @@
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag
package docs
import "github.com/swaggo/swag"
import (
"bytes"
"encoding/json"
"strings"
const docTemplate_swagger = `{
"github.com/alecthomas/template"
"github.com/swaggo/swag"
)
var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
@ -16,634 +24,6 @@ const docTemplate_swagger = `{
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/attachments": {
"get": {
"description": "此接口用来获取文件列表支持type、author_id、container查询",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "文件列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "文件类型avatar、attachment",
"name": "type",
"in": "query"
},
{
"type": "integer",
"description": "作者id",
"name": "author_id",
"in": "query"
},
{
"type": "integer",
"description": "所属模型id",
"name": "container_id",
"in": "query"
},
{
"type": "string",
"description": "所属模型类型",
"name": "container_type",
"in": "query"
},
{
"type": "string",
"description": "排序字段",
"name": "sort_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Attachment"
}
}
}
},
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "新增一个文件",
"parameters": [
{
"type": "file",
"description": "文件",
"name": "file",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "类型可选avatar、attachment",
"name": "type",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "模型id",
"name": "container_id",
"in": "formData"
},
{
"type": "string",
"description": "模型类型",
"name": "container_type",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/service.AttachmentInfo"
}
}
}
}
},
"/attachments/{id}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "获取单个文件",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Attachment"
}
}
}
}
},
"/code/count": {
"get": {
"description": "此接口用来获取代码员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "代码列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"maxLength": 100,
"type": "string",
"description": "名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"description": "排序字段",
"name": "sort_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/create": {
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "新增一条代码数据",
"parameters": [
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "代码包",
"name": "zip_url",
"in": "formData"
},
{
"type": "string",
"description": "证书",
"name": "certificate",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/{id}": {
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "修改代码数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "代码包",
"name": "zip_url",
"in": "formData"
},
{
"type": "string",
"description": "证书",
"name": "certificate",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/{name}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "获取代码详情",
"parameters": [
{
"type": "string",
"description": "代码名",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
},
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "删除代码数据",
"parameters": [
{
"type": "string",
"description": "名称",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/patent/create": {
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "新增专利数据",
"parameters": [
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "title",
"in": "formData",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "专利号",
"name": "number",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "公告日",
"name": "announce_date",
"in": "formData"
},
{
"type": "string",
"description": "公告号",
"name": "announce_number",
"in": "formData"
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "说明书",
"name": "manual",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/patent/list": {
"get": {
"description": "此接口用来获取专利数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "专利列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "标题",
"name": "title",
"in": "query"
},
{
"type": "string",
"description": "排序手段",
"name": "sory_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/patent/{id}": {
"get": {
"description": "根据专利号或者专利名来获取专利详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "获取单个专利",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
},
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "删除一条专利数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.GormResponse"
}
}
}
},
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "修改专利数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "title",
"in": "formData"
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "专利号",
"name": "number",
"in": "formData"
},
{
"type": "string",
"description": "公告日",
"name": "announce_date",
"in": "formData"
},
{
"type": "string",
"description": "公告号",
"name": "announce_number",
"in": "formData"
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "说明书",
"name": "manual",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/people": {
"get": {
"description": "此接口用来获取人员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
@ -725,12 +105,6 @@ const docTemplate_swagger = `{
"description": "个性签名",
"name": "signature",
"in": "formData"
},
{
"type": "string",
"description": "头像地址",
"name": "avatar_url",
"in": "formData"
}
],
"responses": {
@ -833,12 +207,6 @@ const docTemplate_swagger = `{
"description": "个性签名",
"name": "signature",
"in": "formData"
},
{
"type": "string",
"description": "头像地址",
"name": "avatar_url",
"in": "formData"
}
],
"responses": {
@ -1056,164 +424,9 @@ const docTemplate_swagger = `{
}
},
"definitions": {
"model.Attachment": {
"type": "object",
"properties": {
"author_id": {
"description": "上传者id",
"type": "integer"
},
"container_id": {
"description": "所属模型id",
"type": "integer"
},
"container_type": {
"description": "所属模型名称",
"type": "string"
},
"created_at": {
"type": "string"
},
"disk_name": {
"description": "文件存储名称",
"type": "string"
},
"downloads": {
"description": "下载数",
"type": "integer"
},
"ext_name": {
"description": "文件后缀名",
"type": "string"
},
"file_name": {
"description": "文件名",
"type": "string"
},
"file_size": {
"description": "文件大小",
"type": "integer"
},
"id": {
"type": "integer"
},
"type": {
"description": "类型",
"type": "string"
},
"updated_at": {
"type": "string"
}
}
},
"model.Code": {
"type": "object",
"properties": {
"certificate": {
"description": "许可证",
"type": "string"
},
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"summary": {
"description": "摘要",
"type": "string"
},
"updated_at": {
"type": "string"
},
"user_name": {
"description": "发布者",
"$ref": "#/definitions/model.Person"
},
"zip_url": {
"description": "压缩包",
"type": "string"
}
}
},
"model.GormResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {},
"message": {
"type": "string"
}
}
},
"model.Patent": {
"type": "object",
"properties": {
"announce_date": {
"description": "公告日",
"type": "string"
},
"announce_number": {
"description": "公告号",
"type": "string"
},
"application_date": {
"description": "申请日",
"type": "string"
},
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"inventors": {
"description": "发明人",
"type": "array",
"items": {
"$ref": "#/definitions/model.Person"
}
},
"manual": {
"description": "说明书",
"type": "string"
},
"number": {
"description": "专利号",
"type": "string"
},
"patentee": {
"description": "专利授权人",
"$ref": "#/definitions/model.Person"
},
"summary": {
"description": "摘要",
"type": "string"
},
"title": {
"description": "专利名称",
"type": "string"
},
"updated_at": {
"type": "string"
},
"user": {
"description": "创建者",
"$ref": "#/definitions/model.Person"
}
}
},
"model.Person": {
"type": "object",
"properties": {
"avatar_url": {
"type": "string"
},
"created_at": {
"type": "string"
},
@ -1256,45 +469,53 @@ const docTemplate_swagger = `{
"type": "integer"
}
}
},
"service.AttachmentInfo": {
"type": "object",
"properties": {
"disk_name": {
"type": "string"
},
"file_name": {
"type": "string"
},
"file_size": {
"type": "integer"
},
"id": {
"type": "integer"
},
"type": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
}
}`
// SwaggerInfo_swagger holds exported Swagger Info so clients can modify it
var SwaggerInfo_swagger = &swag.Spec{
Version: "1.0",
Host: "",
BasePath: "/api",
Schemes: []string{},
Title: "ShareKnowledgeServer Api",
Description: "知识共享平台",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate_swagger,
type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "1.0",
Host: "",
BasePath: "/api",
Schemes: []string{},
Title: "ShareKnowledgeServer Api",
Description: "知识共享平台",
}
type s struct{}
func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}
return tpl.String()
}
func init() {
swag.Register(SwaggerInfo_swagger.InstanceName(), SwaggerInfo_swagger)
swag.Register(swag.Name, &s{})
}

View File

@ -8,634 +8,6 @@
},
"basePath": "/api",
"paths": {
"/attachments": {
"get": {
"description": "此接口用来获取文件列表支持type、author_id、container查询",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "文件列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "文件类型avatar、attachment",
"name": "type",
"in": "query"
},
{
"type": "integer",
"description": "作者id",
"name": "author_id",
"in": "query"
},
{
"type": "integer",
"description": "所属模型id",
"name": "container_id",
"in": "query"
},
{
"type": "string",
"description": "所属模型类型",
"name": "container_type",
"in": "query"
},
{
"type": "string",
"description": "排序字段",
"name": "sort_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Attachment"
}
}
}
},
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "新增一个文件",
"parameters": [
{
"type": "file",
"description": "文件",
"name": "file",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "类型可选avatar、attachment",
"name": "type",
"in": "formData",
"required": true
},
{
"type": "integer",
"description": "模型id",
"name": "container_id",
"in": "formData"
},
{
"type": "string",
"description": "模型类型",
"name": "container_type",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/service.AttachmentInfo"
}
}
}
}
},
"/attachments/{id}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"attachments"
],
"summary": "获取单个文件",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Attachment"
}
}
}
}
},
"/code/count": {
"get": {
"description": "此接口用来获取代码员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "代码列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"maxLength": 100,
"type": "string",
"description": "名称",
"name": "name",
"in": "query"
},
{
"type": "string",
"description": "排序字段",
"name": "sort_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/create": {
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "新增一条代码数据",
"parameters": [
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "代码包",
"name": "zip_url",
"in": "formData"
},
{
"type": "string",
"description": "证书",
"name": "certificate",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/{id}": {
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "修改代码数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "name",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "代码包",
"name": "zip_url",
"in": "formData"
},
{
"type": "string",
"description": "证书",
"name": "certificate",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/code/{name}": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "获取代码详情",
"parameters": [
{
"type": "string",
"description": "代码名",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
},
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"code"
],
"summary": "删除代码数据",
"parameters": [
{
"type": "string",
"description": "名称",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Code"
}
}
}
}
},
"/patent/create": {
"post": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "新增专利数据",
"parameters": [
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "title",
"in": "formData",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "专利号",
"name": "number",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "公告日",
"name": "announce_date",
"in": "formData"
},
{
"type": "string",
"description": "公告号",
"name": "announce_number",
"in": "formData"
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "说明书",
"name": "manual",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/patent/list": {
"get": {
"description": "此接口用来获取专利数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "专利列表",
"parameters": [
{
"type": "integer",
"description": "页码",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "每页数量",
"name": "page_size",
"in": "query"
},
{
"type": "string",
"description": "标题",
"name": "title",
"in": "query"
},
{
"type": "string",
"description": "排序手段",
"name": "sory_by",
"in": "query"
},
{
"type": "string",
"description": "排序类型",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/patent/{id}": {
"get": {
"description": "根据专利号或者专利名来获取专利详情",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "获取单个专利",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
},
"delete": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "删除一条专利数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.GormResponse"
}
}
}
},
"patch": {
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json"
],
"tags": [
"patent"
],
"summary": "修改专利数据",
"parameters": [
{
"type": "integer",
"description": "ID",
"name": "id",
"in": "path",
"required": true
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "名称",
"name": "title",
"in": "formData"
},
{
"maxLength": 100,
"minLength": 1,
"type": "string",
"description": "专利号",
"name": "number",
"in": "formData"
},
{
"type": "string",
"description": "公告日",
"name": "announce_date",
"in": "formData"
},
{
"type": "string",
"description": "公告号",
"name": "announce_number",
"in": "formData"
},
{
"type": "string",
"description": "摘要",
"name": "summary",
"in": "formData"
},
{
"type": "string",
"description": "说明书",
"name": "manual",
"in": "formData"
}
],
"responses": {
"200": {
"description": "请求成功",
"schema": {
"$ref": "#/definitions/model.Patent"
}
}
}
}
},
"/people": {
"get": {
"description": "此接口用来获取人员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)",
@ -717,12 +89,6 @@
"description": "个性签名",
"name": "signature",
"in": "formData"
},
{
"type": "string",
"description": "头像地址",
"name": "avatar_url",
"in": "formData"
}
],
"responses": {
@ -825,12 +191,6 @@
"description": "个性签名",
"name": "signature",
"in": "formData"
},
{
"type": "string",
"description": "头像地址",
"name": "avatar_url",
"in": "formData"
}
],
"responses": {
@ -1048,164 +408,9 @@
}
},
"definitions": {
"model.Attachment": {
"type": "object",
"properties": {
"author_id": {
"description": "上传者id",
"type": "integer"
},
"container_id": {
"description": "所属模型id",
"type": "integer"
},
"container_type": {
"description": "所属模型名称",
"type": "string"
},
"created_at": {
"type": "string"
},
"disk_name": {
"description": "文件存储名称",
"type": "string"
},
"downloads": {
"description": "下载数",
"type": "integer"
},
"ext_name": {
"description": "文件后缀名",
"type": "string"
},
"file_name": {
"description": "文件名",
"type": "string"
},
"file_size": {
"description": "文件大小",
"type": "integer"
},
"id": {
"type": "integer"
},
"type": {
"description": "类型",
"type": "string"
},
"updated_at": {
"type": "string"
}
}
},
"model.Code": {
"type": "object",
"properties": {
"certificate": {
"description": "许可证",
"type": "string"
},
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"summary": {
"description": "摘要",
"type": "string"
},
"updated_at": {
"type": "string"
},
"user_name": {
"description": "发布者",
"$ref": "#/definitions/model.Person"
},
"zip_url": {
"description": "压缩包",
"type": "string"
}
}
},
"model.GormResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"data": {},
"message": {
"type": "string"
}
}
},
"model.Patent": {
"type": "object",
"properties": {
"announce_date": {
"description": "公告日",
"type": "string"
},
"announce_number": {
"description": "公告号",
"type": "string"
},
"application_date": {
"description": "申请日",
"type": "string"
},
"created_at": {
"type": "string"
},
"id": {
"type": "integer"
},
"inventors": {
"description": "发明人",
"type": "array",
"items": {
"$ref": "#/definitions/model.Person"
}
},
"manual": {
"description": "说明书",
"type": "string"
},
"number": {
"description": "专利号",
"type": "string"
},
"patentee": {
"description": "专利授权人",
"$ref": "#/definitions/model.Person"
},
"summary": {
"description": "摘要",
"type": "string"
},
"title": {
"description": "专利名称",
"type": "string"
},
"updated_at": {
"type": "string"
},
"user": {
"description": "创建者",
"$ref": "#/definitions/model.Person"
}
}
},
"model.Person": {
"type": "object",
"properties": {
"avatar_url": {
"type": "string"
},
"created_at": {
"type": "string"
},
@ -1248,29 +453,6 @@
"type": "integer"
}
}
},
"service.AttachmentInfo": {
"type": "object",
"properties": {
"disk_name": {
"type": "string"
},
"file_name": {
"type": "string"
},
"file_size": {
"type": "integer"
},
"id": {
"type": "integer"
},
"type": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
}
}

View File

@ -1,117 +1,7 @@
basePath: /api
definitions:
model.Attachment:
properties:
author_id:
description: 上传者id
type: integer
container_id:
description: 所属模型id
type: integer
container_type:
description: 所属模型名称
type: string
created_at:
type: string
disk_name:
description: 文件存储名称
type: string
downloads:
description: 下载数
type: integer
ext_name:
description: 文件后缀名
type: string
file_name:
description: 文件名
type: string
file_size:
description: 文件大小
type: integer
id:
type: integer
type:
description: 类型
type: string
updated_at:
type: string
type: object
model.Code:
properties:
certificate:
description: 许可证
type: string
created_at:
type: string
id:
type: integer
name:
type: string
summary:
description: 摘要
type: string
updated_at:
type: string
user_name:
$ref: '#/definitions/model.Person'
description: 发布者
zip_url:
description: 压缩包
type: string
type: object
model.GormResponse:
properties:
code:
type: integer
data: {}
message:
type: string
type: object
model.Patent:
properties:
announce_date:
description: 公告日
type: string
announce_number:
description: 公告号
type: string
application_date:
description: 申请日
type: string
created_at:
type: string
id:
type: integer
inventors:
description: 发明人
items:
$ref: '#/definitions/model.Person'
type: array
manual:
description: 说明书
type: string
number:
description: 专利号
type: string
patentee:
$ref: '#/definitions/model.Person'
description: 专利授权人
summary:
description: 摘要
type: string
title:
description: 专利名称
type: string
updated_at:
type: string
user:
$ref: '#/definitions/model.Person'
description: 创建者
type: object
model.Person:
properties:
avatar_url:
type: string
created_at:
type: string
followed_count:
@ -141,444 +31,12 @@ definitions:
uuid:
type: integer
type: object
service.AttachmentInfo:
properties:
disk_name:
type: string
file_name:
type: string
file_size:
type: integer
id:
type: integer
type:
type: string
url:
type: string
type: object
info:
contact: {}
description: 知识共享平台
title: ShareKnowledgeServer Api
version: "1.0"
paths:
/attachments:
get:
consumes:
- application/json
description: 此接口用来获取文件列表支持type、author_id、container查询
parameters:
- description: 页码
in: query
name: page
type: integer
- description: 每页数量
in: query
name: page_size
type: integer
- description: 文件类型avatar、attachment
in: query
name: type
type: string
- description: 作者id
in: query
name: author_id
type: integer
- description: 所属模型id
in: query
name: container_id
type: integer
- description: 所属模型类型
in: query
name: container_type
type: string
- description: 排序字段
in: query
name: sort_by
type: string
- description: 排序类型
in: query
name: sort_direction
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Attachment'
summary: 文件列表
tags:
- attachments
post:
consumes:
- multipart/form-data
parameters:
- description: 文件
in: formData
name: file
required: true
type: file
- description: 类型可选avatar、attachment
in: formData
name: type
required: true
type: string
- description: 模型id
in: formData
name: container_id
type: integer
- description: 模型类型
in: formData
name: container_type
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/service.AttachmentInfo'
summary: 新增一个文件
tags:
- attachments
/attachments/{id}:
get:
consumes:
- application/json
parameters:
- description: ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Attachment'
summary: 获取单个文件
tags:
- attachments
/code/{id}:
patch:
consumes:
- multipart/form-data
parameters:
- description: ID
in: path
name: id
required: true
type: integer
- description: 名称
in: formData
maxLength: 100
minLength: 1
name: name
required: true
type: string
- description: 摘要
in: formData
name: summary
type: string
- description: 代码包
in: formData
name: zip_url
type: string
- description: 证书
in: formData
name: certificate
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Code'
summary: 修改代码数据
tags:
- code
/code/{name}:
delete:
consumes:
- application/json
parameters:
- description: 名称
in: path
name: name
required: true
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Code'
summary: 删除代码数据
tags:
- code
get:
consumes:
- application/json
parameters:
- description: 代码名
in: path
name: name
required: true
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Code'
summary: 获取代码详情
tags:
- code
/code/count:
get:
consumes:
- application/json
description: 此接口用来获取代码员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)
parameters:
- description: 页码
in: query
name: page
type: integer
- description: 每页数量
in: query
name: page_size
type: integer
- description: 名称
in: query
maxLength: 100
name: name
type: string
- description: 排序字段
in: query
name: sort_by
type: string
- description: 排序类型
in: query
name: sort_direction
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Code'
summary: 代码列表
tags:
- code
/code/create:
post:
consumes:
- multipart/form-data
parameters:
- description: 名称
in: formData
maxLength: 100
minLength: 1
name: name
required: true
type: string
- description: 摘要
in: formData
name: summary
type: string
- description: 代码包
in: formData
name: zip_url
type: string
- description: 证书
in: formData
name: certificate
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Code'
summary: 新增一条代码数据
tags:
- code
/patent/{id}:
delete:
consumes:
- application/json
parameters:
- description: ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.GormResponse'
summary: 删除一条专利数据
tags:
- patent
get:
consumes:
- application/json
description: 根据专利号或者专利名来获取专利详情
parameters:
- description: ID
in: path
name: id
required: true
type: integer
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Patent'
summary: 获取单个专利
tags:
- patent
patch:
consumes:
- multipart/form-data
parameters:
- description: ID
in: path
name: id
required: true
type: integer
- description: 名称
in: formData
maxLength: 100
minLength: 1
name: title
type: string
- description: 专利号
in: formData
maxLength: 100
minLength: 1
name: number
type: string
- description: 公告日
in: formData
name: announce_date
type: string
- description: 公告号
in: formData
name: announce_number
type: string
- description: 摘要
in: formData
name: summary
type: string
- description: 说明书
in: formData
name: manual
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Patent'
summary: 修改专利数据
tags:
- patent
/patent/create:
post:
consumes:
- multipart/form-data
parameters:
- description: 名称
in: formData
maxLength: 100
minLength: 1
name: title
required: true
type: string
- description: 专利号
in: formData
maxLength: 100
minLength: 1
name: number
required: true
type: string
- description: 公告日
in: formData
name: announce_date
type: string
- description: 公告号
in: formData
name: announce_number
type: string
- description: 摘要
in: formData
name: summary
type: string
- description: 说明书
in: formData
name: manual
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Patent'
summary: 新增专利数据
tags:
- patent
/patent/list:
get:
consumes:
- application/json
description: 此接口用来获取专利数据列表,支持名称模糊查询,以及自定义排序(name, created_at)
parameters:
- description: 页码
in: query
name: page
type: integer
- description: 每页数量
in: query
name: page_size
type: integer
- description: 标题
in: query
name: title
type: string
- description: 排序手段
in: query
name: sory_by
type: string
- description: 排序类型
in: query
name: sort_direction
type: string
produces:
- application/json
responses:
"200":
description: 请求成功
schema:
$ref: '#/definitions/model.Patent'
summary: 专利列表
tags:
- patent
/people:
get:
consumes:
@ -631,10 +89,6 @@ paths:
in: formData
name: signature
type: string
- description: 头像地址
in: formData
name: avatar_url
type: string
produces:
- application/json
responses:
@ -702,10 +156,6 @@ paths:
in: formData
name: signature
type: string
- description: 头像地址
in: formData
name: avatar_url
type: string
produces:
- application/json
responses:

View File

@ -8,10 +8,10 @@ package global
import (
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
"github.com/spf13/viper"
"gitlink.org.cn/Gitlink/share-knowledge-server/config"
"go.uber.org/zap"
"gorm.io/gorm"
)
var (

21
go.mod
View File

@ -3,14 +3,15 @@ module gitlink.org.cn/Gitlink/share-knowledge-server
go 1.15
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/eddycjy/opentracing-gorm v0.0.0-20200209122056-516a807d2182
github.com/fsnotify/fsnotify v1.4.9
github.com/gin-gonic/gin v1.7.7
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-playground/locales v0.13.0
github.com/go-playground/universal-translator v0.17.0
github.com/go-playground/validator/v10 v10.6.1
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/go-sql-driver/mysql v1.5.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-migrate/migrate/v4 v4.15.1
github.com/golang/protobuf v1.5.2
@ -18,24 +19,18 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/lib/pq v1.10.2
github.com/mailru/easyjson v0.7.7 // indirect
github.com/lib/pq v1.10.0
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/robfig/cron/v3 v3.0.0
github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect
github.com/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.9
github.com/swaggo/swag v1.7.0
github.com/viletyy/yolk v1.0.1
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/zap v1.20.0
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/tools v0.1.9 // indirect
go.uber.org/zap v1.17.0
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.27.1
gorm.io/driver/mysql v1.2.3
gorm.io/driver/postgres v1.2.3
gorm.io/gorm v1.22.5
moul.io/zapgorm2 v1.1.2
)

View File

@ -9,12 +9,11 @@ package initialize
import (
"fmt"
otgorm "github.com/eddycjy/opentracing-gorm"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"moul.io/zapgorm2"
)
func Gorm() *gorm.DB {
@ -29,8 +28,7 @@ func Gorm() *gorm.DB {
}
func GormMysql() *gorm.DB {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Password, global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.Port, global.GO_CONFIG.Database.Name)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
db, err := gorm.Open("mysql", fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Password, global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.Port, global.GO_CONFIG.Database.Name))
if err != nil {
global.GO_LOG.Error(fmt.Sprintf("Mysql Gorm Open Error: %v", err))
}
@ -39,8 +37,7 @@ func GormMysql() *gorm.DB {
}
func GormPostgresql() *gorm.DB {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Password, global.GO_CONFIG.Database.Name, global.GO_CONFIG.Database.Port)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
db, err := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s port=%d sslmode=disable password=%s", global.GO_CONFIG.Database.Host, global.GO_CONFIG.Database.User, global.GO_CONFIG.Database.Name, global.GO_CONFIG.Database.Port, global.GO_CONFIG.Database.Password))
if err != nil {
global.GO_LOG.Error(fmt.Sprintf("Postgresql Gorm Open Error: %v", err))
}
@ -49,21 +46,25 @@ func GormPostgresql() *gorm.DB {
}
func GormSet(db *gorm.DB) {
if global.GO_CONFIG.App.RunMode != "debug" {
logger := zapgorm2.New(global.GO_LOG)
logger.SetAsDefault()
logger.LogLevel = gormlogger.Info
db.Logger = logger
// 设置表前缀
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return global.GO_CONFIG.Database.TablePrefix + defaultTableName
}
sqlDB, err := db.DB()
if err != nil {
global.GO_LOG.Error(fmt.Sprintf("Gorm setting db.DB(): %v ", err))
// 设置日志
if global.GO_CONFIG.App.RunMode == "debug" {
db.LogMode(true)
}
// 设置迁移
db.AutoMigrate()
// 设置空闲连接池中的最大连接数
sqlDB.SetConnMaxIdleTime(10)
db.DB().SetMaxIdleConns(10)
// 设置打开数据库连接的最大数量
sqlDB.SetMaxOpenConns(100)
db.DB().SetMaxOpenConns(100)
// 设置链路追踪
otgorm.AddGormCallbacks(db)
}

View File

@ -63,14 +63,6 @@ func loadEnvConfig() {
global.GO_CONFIG.App.DefaultContextTimeout = app_default_context_timeout
}
if os.Getenv("APP_UPLOAD_SAVE_PATH") != "" {
global.GO_CONFIG.App.UploadSavePath = os.Getenv("APP_UPLOAD_SAVE_PATH")
}
if os.Getenv("APP_UPLOAD_SERVER_PATH") != "" {
global.GO_CONFIG.App.UploadServerPath = os.Getenv("APP_UPLOAD_SERVER_PATH")
}
app_page_size, _ := convert.StrTo(os.Getenv("APP_PAGE_SISE")).Int64()
if app_page_size > 0 {
global.GO_CONFIG.App.PageSize = app_page_size

View File

@ -1,146 +0,0 @@
package api
import (
"github.com/gin-gonic/gin"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/service"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/errcode"
)
type Attachment struct {
*Base
}
func NewAttachment(base *Base) Attachment {
return Attachment{Base: base}
}
// @Summary 文件列表
// @Tags attachments
// @Description 此接口用来获取文件列表支持type、author_id、container查询
// @Accept json
// @Produce json
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param type query string false "文件类型avatar、attachment"
// @Param author_id query int false "作者id"
// @Param container_id query int false "所属模型id"
// @Param container_type query string false "所属模型类型"
// @Param sort_by query string false "排序字段"
// @Param sort_direction query string false "排序类型"
// @Success 200 {object} model.Attachment "请求成功"
// @Router /attachments [get]
func (a Attachment) List(c *gin.Context) {
param := service.GetAttachmentListRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Error())
return
}
svc := service.New(c.Request.Context())
pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
total, err := svc.CountAttachment(
&service.CountAttachmentRequest{
Type: param.Type,
AuthorID: param.AuthorID,
ContainerID: param.ContainerID,
ContainerType: param.ContainerType,
},
)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CountAttachment err: %v", err)
response.ToErrorResponse(errcode.ErrorCountFail)
return
}
attachments, err := svc.GetAttachmentList(&param, &pager)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetAttachmentList err: %v", err)
response.ToErrorResponse(errcode.ErrorGetListFail)
return
}
response.ToResponseList(attachments, total)
}
// @Summary 获取单个文件
// @Tags attachments
// @Description
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} model.Attachment "请求成功"
// @Router /attachments/{id} [get]
func (a Attachment) Get(c *gin.Context) {
personID, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.GetAttachmentRequest{ID: personID}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbAttachment, err := svc.GetAttachment(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetAttachment err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
}
response.ToResponse(dbAttachment)
}
// @Summary 新增一个文件
// @Tags attachments
// @Description
// @Accept mpfd
// @produce json
// @Param file formData file true "文件"
// @Param type formData string true "类型可选avatar、attachment"
// @Param container_id formData int false "模型id"
// @Param container_type formData string false "模型类型"
// @Success 200 {object} service.AttachmentInfo "请求成功"
// @Router /attachments [post]
func (a Attachment) Create(c *gin.Context) {
response := app.NewResponse(c)
file, fileHeader, err := c.Request.FormFile("file")
if err != nil {
global.GO_LOG.Sugar().Errorf("c.Request.FormFile err: %v", err)
errorCode := errcode.InvalidParams
errorCode.WithData(err)
response.ToErrorResponse(errorCode)
return
}
param := service.CreateAttachmentRequest{}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
attachmentInfo, err := svc.CreateAttachment(&param, file, fileHeader)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CreateAttachment err: %v", err)
errorCode := errcode.UploadAttachmentFail
errorCode.WithData(err)
response.ToErrorResponse(errorCode)
return
}
response.ToResponse(attachmentInfo)
}

View File

@ -1,213 +0,0 @@
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/service"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/errcode"
)
type Code struct {
*Base
}
func NewCode(base *Base) Code {
return Code{Base: base}
}
// @Summary 代码列表
// @Tags code
// @Description 此接口用来获取代码员数据列表,支持名称模糊查询,以及自定义排序(name, created_at)
// @Accept json
// @Produce json
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param name query string false "名称" maxlength(100)
// @Param sort_by query string false "排序字段"
// @Param sort_direction query string false "排序类型"
// @Success 200 {object} model.Code "请求成功"
// @Router /code/count [get]
func (code Code) ListCode(c *gin.Context) {
param := service.GetCodeListRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Error())
return
}
svc := service.New(c.Request.Context())
pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
total, err := svc.CountPerson(&service.CountPersonRequest{Name: param.Name})
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CountPerson err: %v", err)
response.ToErrorResponse(errcode.ErrorCountFail)
return
}
codes, err := svc.GetCodeList(&param, &pager)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetPersonList err: %v", err)
response.ToErrorResponse(errcode.ErrorGetListFail)
return
}
response.ToResponseList(codes, total)
}
// @Summary 新增一条代码数据
// @Tags code
// @Description
// @Accept mpfd
// @Produce json
// @Param name formData string true "名称" minlength(1) maxlength(100)
// @Param summary formData string false "摘要"
// @Param zip_url formData string false "代码包"
// @Param certificate formData string false "证书"
// @Success 200 {object} model.Code "请求成功"
// @Router /code/create [post]
func (code Code) CreateCode(c *gin.Context) {
param := service.CreateCodeRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbCode, err := svc.CreateCode(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CreatePerson err: %v", err)
response.ToErrorResponse(errcode.ErrorCreateFail)
return
}
response.ToResponse(dbCode)
}
// @Summary 获取代码详情
// @Tags code
// @Description
// @Accept json
// @Produce json
// @Param name path string true "代码名"
// @Success 200 {object} model.Code "请求成功"
// @Router /code/{name} [get]
func (code Code) GetCode(c *gin.Context) {
codeName := convert.StrTo(c.Param("name")).String()
fmt.Println("codeName = ", codeName)
response := app.NewResponse(c)
// if err != nil {
// global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
// response.ToErrorResponse(errcode.InvalidParams)
// return
// }
param := service.GetCodeRequest{Name: codeName}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbCode, err := svc.GetCode(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetTemplate err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
}
response.ToResponse(dbCode)
}
// @Summary 修改代码数据
// @Tags code
// @Description
// @Accept mpfd
// @Produce json
// @Param id path int true "ID"
// @Param name formData string true "名称" minlength(1) maxlength(100)
// @Param summary formData string false "摘要"
// @Param zip_url formData string false "代码包"
// @Param certificate formData string false "证书"
// @Success 200 {object} model.Code "请求成功"
// @Router /code/{id} [patch]
func (code Code) UpdateCode(c *gin.Context) {
codeID, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.UpdateCodeRequest{
ID: codeID,
}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs)
return
}
svc := service.New(c.Request.Context())
dbCode, err := svc.UpdateCode(&param)
if err != nil {
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.UpdateCode err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.UpdateCode err: %v", err)
response.ToErrorResponse(errcode.ErrorUpdateFail)
return
}
}
response.ToResponse(dbCode)
}
// @Summary 删除代码数据
// @Tags code
// @Description
// @Accept json
// @Produce json
// @Param name path string true "名称"
// @Success 200 {object} model.Code "请求成功"
// @Router /code/{name} [delete]
func (code Code) DeleteCode(c *gin.Context) {
// codeID, err := convert.StrTo(c.Param("id")).Int64()
codeName := convert.StrTo(c.Param("name")).String()
response := app.NewResponse(c)
// if err != nil {
// global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
// response.ToErrorResponse(errcode.InvalidParams)
// return
// }
param := service.DeleteCodeRequest{Name: codeName}
svc := service.New(c.Request.Context())
dbCode, err := svc.DeleteCode(&param)
if err != nil {
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.DeletePerson err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.DeletePerson err: %v", err)
response.ToErrorResponse(errcode.ErrorDeleteFail)
return
}
}
errcode.Success.Data = dbCode
response.ToErrorResponse(errcode.Success)
}

View File

@ -1,224 +0,0 @@
/*
* @Descripttion:
* @Author: hang
* @version:
* @Date: 2022-02-14 15:57:26
* @LastEditors: hang
* @LastEditTime: 2022-02-17 14:36:14
*/
package api
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/service"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/errcode"
)
type GormResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type Patent struct {
*Base
}
func NewPatent(base *Base) Patent {
return Patent{Base: base}
}
// @Summary 专利列表
// @Tags patent
// @Description 此接口用来获取专利数据列表,支持名称模糊查询,以及自定义排序(name, created_at)
// @Accept json
// @Produce json
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Param title query string false "标题"
// @Param sory_by query string false "排序手段"
// @Param sort_direction query string false "排序类型"
// @Success 200 {object} model.Patent "请求成功"
// @Router /patent/list [get]
func (patent Patent) ListPatent(c *gin.Context) {
param := service.GetPatentListRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs : %v", errs)
response.ToResponseErrors(errs.Error())
return
}
svc := service.New(c.Request.Context())
pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
total, err := svc.CountPatent(&service.CountPatentRequest{Title: param.Title})
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CountPatent err : %v", err)
response.ToErrorResponse(errcode.ErrorCountFail)
return
}
patentTotal, err := svc.GetPatentList(&param, &pager)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetPatentList errr : %v", err)
response.ToErrorResponse(errcode.ErrorGetListFail)
return
}
response.ToResponseList(patentTotal, total)
}
// @Summary 获取单个专利
// @Tags patent
// @Description 根据专利号或者专利名来获取专利详情
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} model.Patent "请求成功"
// @Router /patent/{id} [get]
func (patent Patent) GetPatent(c *gin.Context) {
patentID, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.GetPatentRequest{ID: patentID}
valid, errs := app.BindAndValid(c, &param) // 绑定数据并验证
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndVlid err: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbpatent, err := svc.GetPatent(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetPatent err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
}
response.ToResponse(dbpatent)
}
// @Summary 新增专利数据
// @Tags patent
// @Description
// @Accept mpfd
// @Produce json
// @Param title formData string true "名称" minlength(1) maxlength(100)
// @Param number formData string true "专利号" minlength(1) maxlength(100)
// @Param announce_date formData string false "公告日"
// @Param announce_number formData string false "公告号"
// @Param summary formData string false "摘要"
// @Param manual formData string false "说明书"
// @Success 200 {object} model.Patent "请求成功"
// @Router /patent/create [post]
func (patent Patent) CreatePatent(c *gin.Context) {
param := service.CreatePatentRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Error("app.BindAndValid err:%v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbPatent, err := svc.CreatePatent(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CreatePatent err: %v", err)
response.ToErrorResponse(errcode.ErrorCreateFail)
return
}
response.ToResponse(dbPatent)
}
// @Summary 修改专利数据
// @Tags patent
// @Description
// @Accept mpfd
// @Produce json
// @Param id path int true "ID"
// @Param title formData string false "名称" minlength(1) maxlength(100)
// @Param number formData string false "专利号" minlength(1) maxlength(100)
// @Param announce_date formData string false "公告日"
// @Param announce_number formData string false "公告号"
// @Param summary formData string false "摘要"
// @Param manual formData string false "说明书"
// @Success 200 {object} model.Patent "请求成功"
// @Router /patent/{id} [patch]
func (patent Patent) UpdatePatent(c *gin.Context) {
patentID, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.UpdatePatentRequest{ID: patentID}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs)
return
}
svc := service.New(c.Request.Context())
dbPatent, err := svc.UpdatePatent(&param)
if err != nil {
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.UpdatePatent err: %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.UpdatePatent err: %v", err)
response.ToErrorResponse(errcode.ErrorUpdateFail)
return
}
}
response.ToResponse(dbPatent)
}
// @Summary 删除一条专利数据
// @Tags patent
// @Description
// @Accept json
// @Produce json
// @Param id path int true "ID"
// @Success 200 {object} model.GormResponse "请求成功"
// @Router /patent/{id} [delete]
func (patent Patent) DeletePatent(c *gin.Context) {
patentID, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err:%v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.DeletePatentRequest{ID: patentID}
svc := service.New(c.Request.Context())
deleteResponse, err := svc.DeletePatent(&param)
if err != nil {
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.DeletePatent err : %v", err)
response.ToErrorResponse(errcode.ErrorGetFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.DeletePatent err : %v", err)
response.ToErrorResponse(errcode.ErrorDeleteFail)
return
}
}
errcode.Success.Data = deleteResponse
response.ToErrorResponse(errcode.Success)
}

View File

@ -2,12 +2,12 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/service"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/errcode"
"gorm.io/gorm"
)
type Person struct {
@ -100,7 +100,6 @@ func (person Person) Get(c *gin.Context) {
// @Produce json
// @Param name formData string true "名称" minlength(1) maxlength(100)
// @Param signature formData string false "个性签名"
// @Param avatar_url formData string false "头像地址"
// @Success 200 {object} model.Person "请求成功"
// @Router /people [post]
func (person Person) Create(c *gin.Context) {
@ -132,7 +131,6 @@ func (person Person) Create(c *gin.Context) {
// @Param id path int true "ID"
// @Param name formData string false "名称" maxlength(100)
// @Param signature formData string false "个性签名"
// @Param avatar_url formData string false "头像地址"
// @Success 200 {object} model.Person "请求成功"
// @Router /people/{id} [patch]
func (person Person) Update(c *gin.Context) {

View File

@ -2,12 +2,12 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/service"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/errcode"
"gorm.io/gorm"
)
type Template struct {

View File

@ -1,40 +0,0 @@
package dao
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
func (d *Dao) CountAttachment(authorID, containerID int64, containerType string) (int64, error) {
attachment := model.Attachment{AuthorID: authorID, ContainerID: containerID, ContainerType: containerType}
return attachment.Count(d.Engine)
}
func (d *Dao) GetAttachmentList(authorID, containerID int64, containerType string, page, pageSize int, sortBy, sortDirection string) ([]model.Attachment, error) {
attachment := model.Attachment{AuthorID: authorID, ContainerID: containerID, ContainerType: containerType}
pageOffset := app.GetPageOffset(page, pageSize)
return attachment.List(d.Engine, pageOffset, pageSize, sortBy, sortDirection)
}
func (d *Dao) GetAttachment(id int64) (model.Attachment, error) {
attachment := model.Attachment{
Model: &model.Model{ID: id},
}
return attachment.Get(d.Engine)
}
func (d *Dao) CreateAttachment(fileType, fileName, extName, diskName string, fileSize int, authorID, containerID int64, containerType string) (model.Attachment, error) {
attachment := model.Attachment{
Type: fileType,
FileName: fileName,
ExtName: extName,
DiskName: diskName,
AuthorID: authorID,
ContainerID: containerID,
ContainerType: containerType,
FileSize: fileSize,
}
return attachment, attachment.Create(d.Engine)
}

View File

@ -1,83 +0,0 @@
/*
* @Descripttion:
* @Author: hang
* @version:
* @Date: 2022-02-16 10:14:30
* @LastEditors: hang
* @LastEditTime: 2022-02-17 10:00:35
*/
package dao
import (
"fmt"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
_ "gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
func (d *Dao) CreateCode(name, summary, zip_url, certificate string) (model.Code, error) {
code := model.Code{
Name: name,
Summary: summary,
Certificate: certificate,
ZipUrl: zip_url,
}
return code, code.CreateCode(d.Engine)
}
func (d *Dao) GetCodeList(name string, page, pageSize int, sortBy, sortDirection string) ([]model.Code, error) {
code := model.Code{Name: name}
pageOffset := app.GetPageOffset(page, pageSize)
return code.ListCode(d.Engine, pageOffset, pageSize, sortBy, sortDirection)
}
func (d *Dao) GetCodeCount(name string) (int64, error) {
code := model.Code{Name: name}
return code.CountCode(d.Engine)
}
func (d *Dao) GetCode(name string) (model.Code, error) {
code := model.Code{
Name: name,
}
return code.GetCodeByName(d.Engine)
}
func (d *Dao) UpdateCode(id int64, name, summary, zip_url, certificate string) (model.Code, error) {
code := model.Code{
Name: name,
Summary: summary,
Certificate: certificate,
ZipUrl: zip_url,
Model: &model.Model{ID: id},
}
dbCode, err := code.GetCodeByID(d.Engine)
fmt.Println("dbCode is ", dbCode)
if err != nil {
fmt.Println("err is ", err)
return code, err
}
if err := code.UpdateCode(d.Engine); err != nil {
return dbCode, err
}
return code, err
}
func (d *Dao) DeleteCode(name string) (model.Code, error) {
code := model.Code{
Name: name,
}
dbCode, err := code.GetCodeByName(d.Engine)
if err != nil {
return code, err
}
return dbCode, code.DeleteCode(d.Engine)
}

View File

@ -6,7 +6,7 @@
*/
package dao
import "gorm.io/gorm"
import "github.com/jinzhu/gorm"
type Dao struct {
Engine *gorm.DB

View File

@ -1,95 +0,0 @@
/*
* @Descripttion:
* @Author: hang
* @version:
* @Date: 2022-02-14 16:38:16
* @LastEditors: hang
* @LastEditTime: 2022-02-17 11:47:07
*/
package dao
import (
"net/http"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
func (dao *Dao) GetPatent(id int64) (model.Patent, error) {
patent := model.Patent{
Model: &model.Model{ID: id},
}
return patent.Get(dao.Engine)
}
func (dao *Dao) CountPatent(title string) (int64, error) {
patent := model.Patent{Title: title}
return patent.Count(dao.Engine)
}
func (dao *Dao) GetPatentList(title string, page, pageSize int, sortBy, sortDirection string) ([]model.Patent, error) {
patent := model.Patent{Title: title}
pageOffSet := app.GetPageOffset(page, pageSize)
return patent.List(dao.Engine, pageOffSet, pageSize, sortBy, sortDirection)
}
func (dao *Dao) CreatePatent(title, number, announce_date, announce_number, summary, manual string) (model.Patent, error) {
patent := model.Patent{
Title: title,
Number: number,
AnnounceDate: announce_date,
AnnounceNumber: announce_number,
Summary: summary,
Manual: manual,
}
return patent, patent.Create(dao.Engine)
}
func (dao *Dao) UpdatePatent(id int64, title, number, announce_date, announce_number, summary, manual string) (model.Patent, error) {
patent := model.Patent{
Title: title,
Number: number,
AnnounceDate: announce_date,
AnnounceNumber: announce_number,
Summary: summary,
Manual: manual,
Model: &model.Model{ID: id},
}
dbPatent, err := patent.Get(dao.Engine)
if err != nil {
return patent, err
}
if err := patent.Update(dao.Engine); err != nil {
return dbPatent, err
}
return patent, err
}
func (dao *Dao) DeletePatent(id int64) (model.GormResponse, error) {
patent := model.Patent{
Model: &model.Model{ID: id},
}
dbPatent, err := patent.Get(dao.Engine)
if err != nil {
return model.GormResponse{
Code: http.StatusNotFound,
Message: "数据不存在",
Data: err,
}, err
}
err = dbPatent.Delete(dao.Engine)
return model.GormResponse{
Code: http.StatusOK,
Message: "删除成功",
Data: "OK",
}, err
}

View File

@ -5,7 +5,7 @@ import (
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
func (d *Dao) CountPerson(name string) (int64, error) {
func (d *Dao) CountPerson(name string) (int, error) {
person := model.Person{Name: name}
return person.Count(d.Engine)
}
@ -24,21 +24,19 @@ func (d *Dao) GetPerson(id int64) (model.Person, error) {
return person.Get(d.Engine)
}
func (d *Dao) CreatePerson(name, signature, avatar_url string) (model.Person, error) {
func (d *Dao) CreatePerson(name, signature string) (model.Person, error) {
person := model.Person{
Name: name,
Signature: signature,
AvatarUrl: avatar_url,
}
return person, person.Create(d.Engine)
}
func (d *Dao) UpdatePerson(id int64, name, signature, avatar_url string) (model.Person, error) {
func (d *Dao) UpdatePerson(id int64, name, signature string) (model.Person, error) {
person := model.Person{
Name: name,
Signature: signature,
AvatarUrl: avatar_url,
Model: &model.Model{ID: id},
}

View File

@ -5,7 +5,7 @@ import (
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
func (d *Dao) CountTemplate(name string, uuid int) (int64, error) {
func (d *Dao) CountTemplate(name string, uuid int) (int, error) {
template := model.Template{Name: name, Uuid: uuid}
return template.Count(d.Engine)
}

View File

@ -12,7 +12,6 @@ import (
"github.com/go-playground/locales/en"
"github.com/go-playground/locales/zh"
"github.com/go-playground/validator/v10"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
ut "github.com/go-playground/universal-translator"
enTranslations "github.com/go-playground/validator/v10/translations/en"
@ -26,30 +25,17 @@ func Translations() gin.HandlerFunc {
trans, _ := uni.GetTranslator(locale)
v, ok := binding.Validator.Engine().(*validator.Validate)
v.SetTagName("validate")
var err error
if ok {
switch locale {
case "zh":
err = zhTranslations.RegisterDefaultTranslations(v, trans)
if err != nil {
global.GO_LOG.Sugar().Fatal("zhTranslations.RegisterDefaultTranslations err: %v", err)
} else {
break
}
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
break
case "en":
err = enTranslations.RegisterDefaultTranslations(v, trans)
if err != nil {
global.GO_LOG.Sugar().Fatal("enTranslations.RegisterDefaultTranslations err: %v", err)
} else {
break
}
_ = enTranslations.RegisterDefaultTranslations(v, trans)
break
default:
err = zhTranslations.RegisterDefaultTranslations(v, trans)
if err != nil {
global.GO_LOG.Sugar().Fatal("zhTranslations.RegisterDefaultTranslations err: %v", err)
} else {
break
}
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
break
}
c.Set("trans", trans)
}

View File

@ -1,81 +0,0 @@
package model
import (
"fmt"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gorm.io/gorm"
)
type Attachment struct {
*Model
Type string `json:"type"` // 类型
FileName string `json:"file_name"` // 文件名
ExtName string `json:"ext_name"` // 文件后缀名
DiskName string `json:"disk_name"` // 文件存储名称
Downloads int `json:"downloads"` // 下载数
AuthorID int64 `json:"author_id"` // 上传者id
ContainerType string `json:"container_type"` // 所属模型名称
ContainerID int64 `json:"container_id"` // 所属模型id
FileSize int `json:"file_size"` // 文件大小
}
func (a Attachment) FileUrl() string {
return fmt.Sprintf("%s%s/%s/%s", global.GO_CONFIG.App.Domain, global.GO_CONFIG.App.UploadServerPath, a.Type, a.DiskName)
}
func (a Attachment) Count(db *gorm.DB) (int64, error) {
var count int64
if a.AuthorID >= 0 {
db = db.Where("author_id = ?", a.AuthorID)
}
if a.Type != "" {
db = db.Where("type = ?", a.Type)
}
if a.ContainerID >= 0 && a.ContainerType != "" {
db = db.Where("container_id = ? AND container_type =?", a.ContainerID, a.ContainerType)
}
if err := db.Model(&a).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func (a Attachment) List(db *gorm.DB, pageOffset, pageSize int, sortBy, sortDirection string) (attachments []Attachment, err error) {
if pageOffset >= 0 && pageSize > 0 {
db = db.Offset(pageOffset).Limit(pageSize)
}
if a.Type != "" {
db = db.Where("type = ?", a.Type)
}
if a.AuthorID >= 0 {
db = db.Where("author_id = ?", a.AuthorID)
}
if a.ContainerID >= 0 && a.ContainerType != "" {
db = db.Where("container_id = ? AND container_type", a.ContainerID, a.ContainerType)
}
if sortBy != "" && sortDirection != "" {
db = db.Order(fmt.Sprintf("%s %s", sortBy, sortDirection))
}
if err = db.Find(&attachments).Error; err != nil {
return nil, err
}
return
}
func (a Attachment) Get(db *gorm.DB) (attachment Attachment, err error) {
if err = db.Where("id = ?", a.ID).First(&attachment).Error; err != nil {
return a, err
}
return attachment, nil
}
func (a *Attachment) Create(db *gorm.DB) error {
return db.Create(a).Error
}

View File

@ -1,83 +0,0 @@
/*
* @Descripttion:
* @Author: hang
* @version:
* @Date: 2022-02-15 15:34:18
* @LastEditors: hang
* @LastEditTime: 2022-02-17 10:27:34
*/
package model
import (
"fmt"
"gorm.io/gorm"
)
type Code struct {
*Model
Name string `json:"name"`
UserName Person `gorm:"foreignKey:Name" json:"user_name"` // 发布者
Summary string `json:"summary"` // 摘要
ZipUrl string `json:"zip_url"` //压缩包
Certificate string `json:"certificate"` // 许可证
// CodeLabel []Label `gorm:"foreignKey:LabelName" json:"code_label"` //代码标签
}
func (c Code) CountCode(db *gorm.DB) (int64, error) {
var count int64
if c.Name != "" {
db = db.Where("name = ?", c.Name)
}
if err := db.Model(&c).Count(&count).Error; err != nil {
return 0, err
}
return count, nil
}
func (c Code) ListCode(db *gorm.DB, pageOffset, pageSize int, sortBy, sortDirection string) (code []Code, err error) {
if pageOffset >= 0 && pageSize > 0 {
db = db.Offset(pageOffset).Limit(pageSize)
}
if c.Name != "" {
db = db.Where("name = ?", c.Name)
}
if sortBy != "" && sortDirection != "" {
db = db.Order(fmt.Sprintf("%s %s", sortBy, sortDirection))
}
if err = db.Find(&code).Error; err != nil {
return nil, err
}
return
}
func (c Code) GetCodeByID(db *gorm.DB) (code Code, err error) {
if err = db.Where("id = ?", c.ID).First(&code).Error; err != nil {
return c, err
}
return code, nil
}
func (c Code) GetCodeByName(db *gorm.DB) (code Code, err error) {
if err = db.Where("name = ?", c.Name).First(&code).Error; err != nil {
return c, err
}
return code, nil
}
func (c *Code) CreateCode(db *gorm.DB) error {
return db.Create(c).Error
}
func (c *Code) UpdateCode(db *gorm.DB) error {
return db.Save(c).Error
}
func (c *Code) DeleteCode(db *gorm.DB) error {
return db.Delete(c).Error
}

View File

@ -1,90 +0,0 @@
/*
* @Descripttion:
* @Author: hang
* @version:
* @Date: 2022-02-14 14:00:02
* @LastEditors: hang
* @LastEditTime: 2022-02-17 16:23:07
*/
package model
import (
"fmt"
"gorm.io/gorm"
)
type Patent struct {
*Model
User Person `gorm:"foreginKey:Name" json:"user"` // 创建者
Inventors []Person `json:"inventors"` // 发明人
Patentee Person `gorm:"foreginKey:Name" json:"patentee` // 专利授权人
Number string `gorm:"unique" json:"number"` // 专利号
Title string `json:"title"` // 专利名称
ApplicationDate string `json:"application_date"` // 申请日
AnnounceDate string `json:"announce_date"` // 公告日
AnnounceNumber string `json:"announce_number"` // 公告号
Summary string `json:"summary"` // 摘要
Manual string `json:"manual"` // 说明书
// PatentLabel []Label `gorm:"foreginKey:Name" json:"patent_label"` // 专利标签
}
type GormResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func (p Patent) Get(db *gorm.DB) (patent Patent, err error) {
if err = db.Where("id = ?", p.ID).First(&patent).Error; err != nil {
return p, err
}
return patent, nil
}
func (p *Patent) Create(db *gorm.DB) error {
return db.Create(&p).Error
}
func (p *Patent) Update(db *gorm.DB) error {
return db.Save(p).Error
}
func (p *Patent) Delete(db *gorm.DB) error {
return db.Delete(&p).Error
}
func (p *Patent) List(db *gorm.DB, pageIndex, pageSize int, sortBy, sortDirection string) (patent []Patent, err error) {
if pageIndex >= 0 && pageSize > 0 {
db = db.Offset(pageIndex).Limit(pageSize)
}
if p.Title != "" {
db = db.Where("title=?", p.Title)
}
if sortBy != "" && sortDirection != "" {
db = db.Order(fmt.Sprintf("%s %s", sortBy, sortDirection))
}
if err = db.Find(&patent).Error; err != nil {
return nil, err
}
return
}
func (p *Patent) Count(db *gorm.DB) (int64, error) {
var count int64
if p.Title != "" {
db = db.Where("title = ?", p.Title)
}
if err := db.Model(&p).Count(&count).Error; err != nil {
return 0, nil
}
return count, nil
}

View File

@ -3,7 +3,7 @@ package model
import (
"fmt"
"gorm.io/gorm"
"github.com/jinzhu/gorm"
)
type Person struct {
@ -11,11 +11,10 @@ type Person struct {
Name string `json:"name"` // 名称
Signature string `json:"signature"` // 个性签名
FollowedCount int `json:"followed_count"` // 关注数
AvatarUrl string `json:"avatar_url"`
}
func (p Person) Count(db *gorm.DB) (int64, error) {
var count int64
func (p Person) Count(db *gorm.DB) (int, error) {
var count int
if p.Name != "" {
db = db.Where("name = ?", p.Name)
}
@ -42,8 +41,8 @@ func (p Person) List(db *gorm.DB, pageOffset, pageSize int, sortBy, sortDirectio
}
func (p Person) Get(db *gorm.DB) (person Person, err error) {
if err = db.Where("id = ?", p.ID).First(&person).Error; err != nil {
return p, err
if notFound := db.Where("id = ?", p.ID).First(&person).RecordNotFound(); notFound {
return p, gorm.ErrRecordNotFound
}
return person, nil
@ -54,7 +53,7 @@ func (p *Person) Create(db *gorm.DB) error {
}
func (p *Person) Update(db *gorm.DB) error {
return db.Save(p).Error
return db.Update(p).Error
}
func (p *Person) Delete(db *gorm.DB) error {

View File

@ -9,7 +9,7 @@ package model
import (
"fmt"
"gorm.io/gorm"
"github.com/jinzhu/gorm"
)
type Template struct {
@ -19,8 +19,8 @@ type Template struct {
Uuid int `json:"uuid"`
}
func (v Template) Count(db *gorm.DB) (int64, error) {
var count int64
func (v Template) Count(db *gorm.DB) (int, error) {
var count int
if v.Name != "" {
db = db.Where("name = ?", v.Name)
}
@ -55,8 +55,8 @@ func (v Template) List(db *gorm.DB, pageOffset, pageSize int, sortBy, sortDirect
}
func (v Template) Get(db *gorm.DB) (vendor Template, err error) {
if err = db.Where("id = ?", v.ID).First(&vendor).Error; err != nil {
return v, err
if notFound := db.Where("id = ?", v.ID).First(&vendor).RecordNotFound(); notFound {
return v, gorm.ErrRecordNotFound
}
return vendor, nil
@ -67,9 +67,10 @@ func (v *Template) Create(db *gorm.DB) error {
}
func (v *Template) Update(db *gorm.DB) error {
return db.Save(v).Error
err := db.Save(v).Error
return err
}
func (v *Template) Delete(db *gorm.DB) error {
return db.Delete(v).Error
return db.Where("id = ?", v.ID).Delete(v).Error
}

View File

@ -1,13 +1,12 @@
/*
* @Date: 2021-03-21 19:54:57
* @LastEditors: hang
* @LastEditTime: 2022-02-14 14:39:28
* @LastEditors: viletyy
* @LastEditTime: 2021-07-09 14:44:37
* @FilePath: /potato/internal/routers/router.go
*/
package routers
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
@ -37,7 +36,6 @@ func InitRouter() *gin.Engine {
} else {
Engine.Use(middleware.AccessLog())
}
Engine.StaticFS(global.GO_CONFIG.App.UploadServerPath, http.Dir(global.GO_CONFIG.App.UploadSavePath))
Engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
@ -54,13 +52,6 @@ func InitRouter() *gin.Engine {
templates.PATCH("/:id", template.Update)
templates.DELETE("/:id", template.Delete)
}
attachments := apiRouterGroup.Group("/attachments")
attachment := api.NewAttachment(&base)
{
attachments.GET("", attachment.List)
attachments.POST("", attachment.Create)
attachments.GET("/:id", attachment.Get)
}
people := apiRouterGroup.Group("/people")
person := api.NewPerson(&base)
{
@ -70,22 +61,6 @@ func InitRouter() *gin.Engine {
people.PATCH("/:id", person.Update)
people.DELETE("/:id", person.Delete)
}
patents := apiRouterGroup.Group("/patent")
patent := api.NewPatent(&base)
{
patents.GET("/:id", patent.GetPatent)
patents.POST("/create", patent.CreatePatent)
patents.PATCH("/:id", patent.UpdatePatent)
}
codes := apiRouterGroup.Group("/code")
code := api.NewCode(&base)
{
codes.GET("/:name", code.GetCode)
codes.POST("/create", code.CreateCode)
codes.PATCH("/:id", code.UpdateCode)
codes.DELETE("/:name", code.DeleteCode)
codes.GET("/count", code.ListCode)
}
return Engine
}

View File

@ -1,106 +0,0 @@
package service
import (
"errors"
"fmt"
"mime/multipart"
"os"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/upload"
)
type CountAttachmentRequest struct {
Type string `form:"type" json:"type"`
AuthorID int64 `form:"author_id" json:"author_id"`
ContainerID int64 `form:"container_id" json:"container_id"`
ContainerType string `form:"container_type" json:"container_type" validate:"max=100"`
}
type GetAttachmentListRequest struct {
Type string `form:"type" json:"type"`
AuthorID int64 `form:"author_id" json:"author_id"`
ContainerID int64 `form:"container_id" json:"container_id"`
ContainerType string `form:"container_type" json:"container_type" validate:"max=100"`
SortBy string `form:"sort_by" json:"sort_by" validate:"oneof='created_at' ''"`
SortDirection string `form:"sort_direction" json:"sort_direction" validate:"oneof='asc' 'desc' ''"`
}
type GetAttachmentRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
}
type CreateAttachmentRequest struct {
Type string `form:"type" json:"type" validate:"oneof='avatar' 'attachment'"`
AuthorID int64 `form:"author_id" json:"author_id"`
ContainerID int64 `form:"container_id" json:"container_id"`
ContainerType string `form:"container_type" json:"container_type"`
}
type AttachmentInfo struct {
ID int64 `json:"id"`
Type string `json:"type"`
FileName string `json:"file_name"`
DiskName string `json:"disk_name"`
Url string `json:"url"`
FileSize int `json:"file_size"`
}
func (svc *Service) CountAttachment(param *CountAttachmentRequest) (int64, error) {
return svc.Dao.CountAttachment(param.AuthorID, param.ContainerID, param.ContainerType)
}
func (svc *Service) GetAttachmentList(param *GetAttachmentListRequest, paper *app.Pager) ([]model.Attachment, error) {
return svc.Dao.GetAttachmentList(param.AuthorID, param.ContainerID, param.ContainerType, paper.Page, paper.PageSize, param.SortBy, param.SortDirection)
}
func (svc *Service) GetAttachment(param *GetAttachmentRequest) (model.Attachment, error) {
return svc.Dao.GetAttachment(param.ID)
}
func (svc *Service) CreateAttachment(param *CreateAttachmentRequest, file multipart.File, fileHeader *multipart.FileHeader) (info AttachmentInfo, err error) {
filename := upload.GetFileName(fileHeader.Filename)
extname := upload.GetFileExt(fileHeader.Filename)
if !upload.CheckContainExt(upload.FileType(param.Type), filename) {
err = errors.New("upload: file suffix is not supported.")
return
}
isPermissionSize, filesize := upload.CheckMaxSize(upload.FileType(param.Type), file)
if !isPermissionSize {
err = errors.New("upload: exceeded maximum file limit.")
return
}
uploadSavePath := upload.GetSavePath()
relativeUploadSavePath := fmt.Sprintf("%s/%s", uploadSavePath, param.Type)
if upload.CheckSavePath(relativeUploadSavePath) {
if createdErr := upload.CreateSavePath(relativeUploadSavePath, os.ModePerm); createdErr != nil {
err = errors.New("upload: failed to create save directory.")
return
}
}
if isPermission, permissionErr := upload.CheckPermission(uploadSavePath); isPermission || permissionErr != nil {
err = errors.New("upload: insufficient file permissions.")
return
}
dst := fmt.Sprintf("%s/%s", relativeUploadSavePath, filename)
if err = upload.SaveFile(fileHeader, dst); err != nil {
return
}
dbAttachment, err := svc.Dao.CreateAttachment(param.Type, fileHeader.Filename, extname, filename, filesize, param.AuthorID, param.ContainerID, param.ContainerType)
if err != nil {
return
}
info.ID = dbAttachment.ID
info.Type = dbAttachment.Type
info.FileName = dbAttachment.FileName
info.DiskName = dbAttachment.DiskName
info.Url = dbAttachment.FileUrl()
info.FileSize = filesize
return
}

View File

@ -1,62 +0,0 @@
package service
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
type CountCodeRequest struct {
Name string `form:"name" json:"name" validate:"max=100"`
}
type GetCodeListRequest struct {
Name string `form:"name" json:"name" validate:"max=100"`
SortBy string `form:"sort_by" json:"sort_by" validate:"oneof='name' 'created_at' ''"`
SortDirection string `form:"sort_direction" json:"sort_direction" validate:"oneof='asc' 'desc' ''"`
}
type CreateCodeRequest struct {
Name string `form:"name" json:"name" validate:"required"`
Summary string `form:"summary" json:"summary"`
ZipUrl string `form:"zip_url" json:"zip_url"`
Certificate string `form:"certificate" json:"certificate"`
}
type GetCodeRequest struct {
Name string `form:"name" json:"name" validate:"required"`
}
type UpdateCodeRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
Name string `form:"name" json:"name"`
Summary string `form:"summary" json:"summary"`
ZipUrl string `form:"zip_url" json:"zip_url"`
Certificate string `form:"certificate" json:"certificate"`
}
type DeleteCodeRequest struct {
Name string `form:"name" json:"name" validate:"required"`
}
func (svc *Service) GetCountCode(param *CountCodeRequest) (int64, error) {
return svc.Dao.GetCodeCount(param.Name)
}
func (svc *Service) GetCodeList(param *GetCodeListRequest, paper *app.Pager) ([]model.Code, error) {
return svc.Dao.GetCodeList(param.Name, paper.Page, paper.PageSize, param.SortBy, param.SortDirection)
}
func (svc *Service) CreateCode(param *CreateCodeRequest) (model.Code, error) {
return svc.Dao.CreateCode(param.Name, param.Summary, param.ZipUrl, param.Certificate)
}
func (svc *Service) GetCode(param *GetCodeRequest) (model.Code, error) {
return svc.Dao.GetCode(param.Name)
}
func (svc *Service) UpdateCode(param *UpdateCodeRequest) (model.Code, error) {
return svc.Dao.UpdateCode(param.ID, param.Name, param.Summary, param.ZipUrl, param.Certificate)
}
func (svc *Service) DeleteCode(param *DeleteCodeRequest) (model.Code, error) {
return svc.Dao.DeleteCode(param.Name)
}

View File

@ -1,82 +0,0 @@
package service
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gitlink.org.cn/Gitlink/share-knowledge-server/pkg/app"
)
type GetPatentRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
}
type CountPatentRequest struct {
Title string `form:"title" json:"title"`
}
type GetPatentListRequest struct {
Title string `form:"title" json:"title"`
SortBy string `form:"sort_by" json:"sort_by" validate:"oneof='name' 'created_at' ''"`
SortDirection string `form:"sort_direction" json:"sort_direction" validate:"oneof='asc' 'desc' ''"`
}
type DeletePatentRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
}
type CreatePatentRequest struct {
Title string `form:"title" json:"title" validate:"required"`
Number string `form:"number" json:"number" validate:"required"`
Announce_date string `form:"announce_date" json:"announce_date"`
Summary string `form:"summary" json:"summary"`
Manual string `form:"manual" json:"manual"`
Announce_number string `form:"announce_number" json:"announce_number"`
}
type UpdatePatentRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
Title string `form:"title" json:"title"`
Number string `form:"number" json:"number"`
Announce_date string `form:"announce_date" json:"announce_date"`
Summary string `form:"summary" json:"summary"`
Manual string `form:"manual" json:"manual"`
Announce_number string `form:"announce_number" json:"announce_number"`
}
func (svc *Service) GetPatent(param *GetPatentRequest) (model.Patent, error) {
return svc.Dao.GetPatent(param.ID)
}
func (svc *Service) CountPatent(param *CountPatentRequest) (int64, error) {
return svc.Dao.CountPatent(param.Title)
}
func (svc *Service) GetPatentList(param *GetPatentListRequest, paper *app.Pager) ([]model.Patent, error) {
return svc.Dao.GetPatentList(param.Title, paper.Page, paper.PageSize, param.SortBy, param.SortDirection)
}
func (svc *Service) CreatePatent(param *CreatePatentRequest) (model.Patent, error) {
return svc.Dao.CreatePatent(
param.Title,
param.Number,
param.Announce_date,
param.Announce_number,
param.Summary,
param.Manual)
}
func (svc *Service) UpdatePatent(param *UpdatePatentRequest) (model.Patent, error) {
return svc.Dao.UpdatePatent(
param.ID,
param.Title,
param.Number,
param.Announce_date,
param.Announce_number,
param.Summary,
param.Manual,
)
}
func (svc *Service) DeletePatent(param *DeletePatentRequest) (model.GormResponse, error) {
return svc.Dao.DeletePatent(param.ID)
}

View File

@ -22,21 +22,19 @@ type GetPersonRequest struct {
type CreatePersonRequest struct {
Name string `form:"name" json:"name" validate:"required"`
Signature string `form:"signature" json:"signature"`
AvatarUrl string `form:"avatar_url" json:"avatar_url" validate:"url"`
}
type UpdatePersonRequest struct {
ID int64 `form:"id" json:"id" validate:"required,gte=1"`
Name string `form:"name" json:"name"`
Signature string `form:"signature" json:"signature"`
AvatarUrl string `form:"avatar_url" json:"avatar_url" validate:"url"`
}
type DeletePersonRequest struct {
ID int64 `json:"id" validate:"required,gte=1"`
}
func (svc *Service) CountPerson(param *CountPersonRequest) (int64, error) {
func (svc *Service) CountPerson(param *CountPersonRequest) (int, error) {
return svc.Dao.CountPerson(param.Name)
}
@ -49,11 +47,11 @@ func (svc *Service) GetPerson(param *GetPersonRequest) (model.Person, error) {
}
func (svc *Service) CreatePerson(param *CreatePersonRequest) (model.Person, error) {
return svc.Dao.CreatePerson(param.Name, param.Signature, param.AvatarUrl)
return svc.Dao.CreatePerson(param.Name, param.Signature)
}
func (svc *Service) UpdatePerson(param *UpdatePersonRequest) (model.Person, error) {
return svc.Dao.UpdatePerson(param.ID, param.Name, param.Signature, param.AvatarUrl)
return svc.Dao.UpdatePerson(param.ID, param.Name, param.Signature)
}
func (svc *Service) DeletePerson(param *DeletePersonRequest) (model.Person, error) {

View File

@ -9,6 +9,7 @@ package service
import (
"context"
otgorm "github.com/eddycjy/opentracing-gorm"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/dao"
)
@ -20,7 +21,7 @@ type Service struct {
func New(ctx context.Context) Service {
svc := Service{Ctx: ctx}
svc.Dao = dao.New(global.GO_DB)
svc.Dao = dao.New(otgorm.WithContext(svc.Ctx, global.GO_DB))
return svc
}

View File

@ -36,7 +36,7 @@ type DeleteTemplateRequest struct {
ID int64 `json:"id" validate:"required,gte=1"`
}
func (svc *Service) CountTemplate(param *CountTemplateRequest) (int64, error) {
func (svc *Service) CountTemplate(param *CountTemplateRequest) (int, error) {
return svc.Dao.CountTemplate(param.Name, param.Uuid)
}

12
main.go
View File

@ -6,7 +6,6 @@ import (
"github.com/viletyy/yolk/convert"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gitlink.org.cn/Gitlink/share-knowledge-server/initialize"
"gitlink.org.cn/Gitlink/share-knowledge-server/migrations"
)
// @title ShareKnowledgeServer Api
@ -22,14 +21,9 @@ func main() {
global.GO_DB = initialize.Gorm()
global.GO_REDIS = initialize.Redis()
go initialize.Cron()
if err := migrations.Migrate(global.GO_DB); err != nil {
global.GO_LOG.Sugar().Fatalf("migrations.Migrate: %v", err)
}
sqlDB, err := global.GO_DB.DB()
if err != nil {
global.GO_LOG.Sugar().Fatalf("global.GO_DB.DB err: %v", err)
}
defer sqlDB.Close()
go initialize.Migrate()
defer global.GO_DB.Close()
defer global.GO_REDIS.Close()
flag.StringVar(&grpcPort, "grpc_port", convert.ToString(global.GO_CONFIG.Server.GrpcPort), "启动grpc服务端口号")

View File

@ -0,0 +1 @@
DROP table IF EXISTS templates;

View File

@ -0,0 +1,10 @@
CREATE TABLE `templates` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`uuid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_templates_deleted_at` (`deleted_at`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

View File

@ -0,0 +1 @@
DROP table IF EXISTS people;

View File

@ -0,0 +1,11 @@
CREATE TABLE `people` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`signature` varchar(255) DEFAULT NULL,
`followed_count` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_people_deleted_at` (`deleted_at`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

View File

@ -1,126 +0,0 @@
package migrations
import (
"fmt"
"log"
"os"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
"gorm.io/gorm"
)
const minDBVersion = 0
type Migration interface {
Description() string
Migrate(*gorm.DB) error
}
type migration struct {
description string
migrate func(*gorm.DB) error
}
func NewMigration(desc string, fn func(*gorm.DB) error) Migration {
return &migration{desc, fn}
}
func (m *migration) Description() string {
return m.description
}
func (m *migration) Migrate(db *gorm.DB) error {
return m.migrate(db)
}
type Version struct {
ID int64 `gorm:"primary_key"`
Version int64
}
var migrations = []Migration{
NewMigration("add table templates", addTemplates),
NewMigration("add table people", addPeople),
NewMigration("add table attachments", addAttachments),
NewMigration("add column avatar_url to people", addColumnAvatarUrlToPeople),
}
func GetCurrentDBVersion(db *gorm.DB) (int64, error) {
if err := db.Debug().AutoMigrate(&Version{}); err != nil {
return -1, fmt.Errorf("db.AutoMigrate: %v", err)
}
currentVersion := &Version{ID: 1}
if err := db.Debug().First(currentVersion).Error; err != nil {
return -1, fmt.Errorf("db.First: %v", err)
}
return currentVersion.Version, nil
}
func ExpectedVersion() int64 {
return int64(minDBVersion + len(migrations))
}
func EnsureUpTodate(db *gorm.DB) error {
currentDB, err := GetCurrentDBVersion(db)
if err != nil {
return err
}
if currentDB < 0 {
return fmt.Errorf("Database has not been initialised")
}
if minDBVersion > currentDB {
return fmt.Errorf("DB version %d (<= %d) is too old for auto-migration.", currentDB, minDBVersion)
}
expected := ExpectedVersion()
if currentDB != expected {
return fmt.Errorf(`Current database version %d is not equal to the expected version %d. `, currentDB, expected)
}
return nil
}
func Migrate(db *gorm.DB) error {
if err := db.AutoMigrate(&Version{}); err != nil {
return fmt.Errorf("db.AutoMigrate: %v", err)
}
currentVersion := &Version{ID: 1}
if err := db.First(currentVersion).Error; err != nil {
currentVersion.Version = 0
if err := db.Debug().Create(currentVersion).Error; err != nil {
return fmt.Errorf("db.Create: %v", err)
}
}
v := currentVersion.Version
if minDBVersion > v {
global.GO_LOG.Fatal("Please upgrade the latest code.")
}
if int(v-minDBVersion) > len(migrations) {
msg := fmt.Sprintf("Downgrading database version from '%d' to '%d' is not supported and may result in loss of data integrity.\nIf you really know what you're doing, execute `UPDATE version SET version=%d WHERE id=1;`\n",
v, minDBVersion+len(migrations), minDBVersion+len(migrations))
fmt.Fprint(os.Stderr, msg)
log.Fatal(msg)
return nil
}
for i, m := range migrations[v-minDBVersion:] {
global.GO_LOG.Sugar().Infof("Migration[%d]: %s", v+int64(i), m.Description())
if err := m.Migrate(db); err != nil {
return fmt.Errorf("Migrate: %v", err)
}
currentVersion.Version = v + int64(i) + 1
if err := db.Save(currentVersion).Error; err != nil {
return err
}
}
return nil
}

View File

@ -1,30 +0,0 @@
package migrations
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gorm.io/gorm"
)
func addTemplates(db *gorm.DB) (err error) {
type Template struct {
*model.Model
Name string
Uuid int
}
if has := db.Migrator().HasTable(&Template{}); !has {
if err := db.Debug().Migrator().CreateTable(&Template{}); err != nil {
return err
}
} else {
if err := db.Debug().Migrator().DropTable(&Template{}); err != nil {
return err
}
if err := db.Debug().Migrator().CreateTable(&Template{}); err != nil {
return err
}
}
return nil
}

View File

@ -1,30 +0,0 @@
package migrations
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gorm.io/gorm"
)
func addPeople(db *gorm.DB) error {
type Person struct {
*model.Model
Name string
Signature string
FollowedCount int
}
if has := db.Migrator().HasTable(&Person{}); !has {
if err := db.Debug().Migrator().CreateTable(&Person{}); err != nil {
return err
}
} else {
if err := db.Debug().Migrator().DropTable(&Person{}); err != nil {
return err
}
if err := db.Debug().Migrator().CreateTable(&Person{}); err != nil {
return err
}
}
return nil
}

View File

@ -1,36 +0,0 @@
package migrations
import (
"gitlink.org.cn/Gitlink/share-knowledge-server/internal/model"
"gorm.io/gorm"
)
func addAttachments(db *gorm.DB) error {
type Attachment struct {
*model.Model
Type string
FileName string
ExtName string
DiskName string
Downloads int
AuthorID int64
ContainerID string
ContainerType int64
FileSize int
}
if has := db.Migrator().HasTable(&Attachment{}); !has {
if err := db.Debug().Migrator().CreateTable(&Attachment{}); err != nil {
return err
}
} else {
if err := db.Debug().Migrator().DropTable(&Attachment{}); err != nil {
return err
}
if err := db.Debug().Migrator().CreateTable(&Attachment{}); err != nil {
return err
}
}
return nil
}

View File

@ -1,15 +0,0 @@
package migrations
import "gorm.io/gorm"
func addColumnAvatarUrlToPeople(db *gorm.DB) error {
type Person struct {
AvatarUrl string
}
if has := db.Migrator().HasColumn(&Person{}, "AvatarUrl"); !has {
if err := db.Debug().Migrator().AddColumn(&Person{}, "AvatarUrl"); err != nil {
return err
}
}
return nil
}

View File

@ -16,9 +16,9 @@ type Response struct {
}
type Pager struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int `json:"total"`
}
func NewResponse(ctx *gin.Context) *Response {
@ -38,7 +38,7 @@ func (r *Response) ToResponseErrors(data interface{}) {
r.ToErrorResponse(err)
}
func (r *Response) ToResponseList(list interface{}, total int64) {
func (r *Response) ToResponseList(list interface{}, total int) {
err := errcode.Success
err.WithData(map[string]interface{}{
"list": list,

View File

@ -17,5 +17,4 @@ var (
UnauthorizedTokenTimeout = NewError(10006, "鉴权失败Token 超时")
UnauthorizedTokenGenerate = NewError(10007, "鉴权失败Token 生成失败")
TooManyRequests = NewError(10008, "请求过多")
UploadAttachmentFail = NewError(10009, "上传文件失败")
)

View File

@ -1,116 +0,0 @@
package upload
import (
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
"strings"
"time"
"github.com/viletyy/yolk/crypt"
"gitlink.org.cn/Gitlink/share-knowledge-server/global"
)
type FileType string
const (
TypeAvatar FileType = "avatar" // 头像
TypeAttachment FileType = "attachment" // 附件
)
func GetFileName(name string) string {
ext := GetFileExt(name)
fileName := strings.TrimSuffix(name, ext)
fileName = crypt.Md5Encode(fmt.Sprintf("%d%s", time.Now().Unix(), fileName))
return fmt.Sprintf("%s%s", fileName, ext)
}
func GetFileExt(name string) string {
return path.Ext(name)
}
func GetSavePath() string {
return global.GO_CONFIG.App.UploadSavePath
}
func CheckSavePath(dst string) bool {
_, err := os.Stat(dst)
return os.IsNotExist(err)
}
func CheckContainExt(t FileType, name string) bool {
ext := GetFileExt(name)
ext = strings.ToUpper(ext)
switch t {
case TypeAvatar:
for _, allowExt := range global.GO_CONFIG.App.UploadAvatarAllowExts {
if strings.ToUpper(allowExt) == ext {
return true
}
}
case TypeAttachment:
for _, allowExt := range global.GO_CONFIG.App.UploadAttachmentAllowExts {
if strings.ToUpper(allowExt) == ext {
return true
}
}
}
return false
}
func CheckMaxSize(t FileType, f multipart.File) (bool, int) {
content, _ := ioutil.ReadAll(f)
size := len(content)
switch t {
case TypeAvatar:
if size <= int(global.GO_CONFIG.App.UploadAvatarMaxSize*1024*1024) {
return true, size
}
case TypeAttachment:
if size <= int(global.GO_CONFIG.App.UploadAttachmentMaxSize*1024*1024) {
return true, size
}
}
return false, 0
}
func CheckPermission(dst string) (bool, error) {
_, err := os.Stat(dst)
if err != nil {
return true, err
}
return os.IsPermission(err), err
}
func CreateSavePath(dst string, perm os.FileMode) error {
err := os.MkdirAll(dst, perm)
if err != nil {
return err
}
return nil
}
func SaveFile(file *multipart.FileHeader, dst string) error {
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, src)
return err
}