doc gen fix

This commit is contained in:
darius 2024-07-04 12:54:47 +02:00
parent c606efd7dc
commit b27f489f8d
6 changed files with 598 additions and 23 deletions

View File

@ -1,6 +1,6 @@
module openAPI module openAPI
go 1.22 go 1.22.2
require ( require (
github.com/a-h/respond v0.0.2 github.com/a-h/respond v0.0.2

View File

@ -4,7 +4,7 @@ import (
"github.com/a-h/respond" "github.com/a-h/respond"
"github.com/a-h/rest" "github.com/a-h/rest"
"net/http" "net/http"
"portfolio/database/ent" "portfolio/api/types"
) )
func RegisterProjectEndpoints() { func RegisterProjectEndpoints() {
@ -21,7 +21,7 @@ func RegisterProjectEndpoints() {
Regexp: `\d+`, Regexp: `\d+`,
}). }).
HasDescription("Update project by id"). HasDescription("Update project by id").
HasRequestModel(rest.ModelOf[ent.Project]()). HasRequestModel(rest.ModelOf[types.Project]()).
HasResponseModel(http.StatusOK, rest.ModelOf[string]()). HasResponseModel(http.StatusOK, rest.ModelOf[string]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()). HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()). HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
@ -30,7 +30,7 @@ func RegisterProjectEndpoints() {
api.Patch("/projects"). api.Patch("/projects").
HasDescription("Update projects WIP"). HasDescription("Update projects WIP").
HasResponseModel(http.StatusOK, rest.ModelOf[[]ent.Project]()). HasResponseModel(http.StatusOK, rest.ModelOf[[]types.Project]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()). HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()). HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]()) HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]())
@ -41,12 +41,12 @@ func RegisterProjectEndpoints() {
Regexp: `\d+`, Regexp: `\d+`,
}). }).
HasDescription("Get project by id"). HasDescription("Get project by id").
HasResponseModel(http.StatusOK, rest.ModelOf[ent.Project]()). HasResponseModel(http.StatusOK, rest.ModelOf[types.Project]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()). HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()) HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Get("/projects"). api.Get("/projects").
HasDescription("Get projects"). HasDescription("Get projects").
HasResponseModel(http.StatusOK, rest.ModelOf[[]ent.Project]()). HasResponseModel(http.StatusOK, rest.ModelOf[[]types.Project]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()) HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
} }

View File

@ -5,7 +5,6 @@ import (
"github.com/a-h/rest" "github.com/a-h/rest"
"net/http" "net/http"
"portfolio/api/types" "portfolio/api/types"
"portfolio/database/ent"
) )
func RegisterUserEndpoints() { func RegisterUserEndpoints() {
@ -15,14 +14,14 @@ func RegisterUserEndpoints() {
Regexp: `\d+`, Regexp: `\d+`,
}). }).
HasDescription("Get user by uid."). HasDescription("Get user by uid.").
HasResponseModel(http.StatusOK, rest.ModelOf[ent.User]()). HasResponseModel(http.StatusOK, rest.ModelOf[types.User]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()). HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()). HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()) HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Post("/register"). api.Post("/register").
HasDescription("Register."). HasDescription("Register.").
HasRequestModel(rest.ModelOf[ent.User]()). HasRequestModel(rest.ModelOf[types.User]()).
HasResponseModel(http.StatusCreated, rest.ModelOf[string]()). HasResponseModel(http.StatusCreated, rest.ModelOf[string]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()). HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()). HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).

10
api/types/projectTypes.go Normal file
View File

@ -0,0 +1,10 @@
package types
type Project struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
ImageURL string `json:"image_url,omitempty"`
DocURL string `json:"doc_url,omitempty"`
}

View File

@ -1,5 +1,9 @@
package types package types
import (
"portfolio/database/ent/user"
)
type Username struct { type Username struct {
Name string Name string
} }
@ -8,3 +12,11 @@ type LoginUser struct {
Email string Email string
Password string Password string
} }
type User struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Password string `json:"-"`
Role user.Role `json:"role,omitempty"`
}

View File

@ -1,26 +1,133 @@
{ {
"components": {}, "components": {
"schemas": {
"Error": {
"properties": {
"message": {
"type": "string"
},
"statusCode": {
"type": "integer"
}
},
"required": [
"message",
"statusCode"
],
"type": "object"
},
"portfolio_api_types_LoginUser": {
"properties": {
"Email": {
"type": "string"
},
"Password": {
"type": "string"
}
},
"required": [
"Email",
"Password"
],
"type": "object"
},
"portfolio_api_types_Project": {
"properties": {
"description": {
"type": "string"
},
"doc_url": {
"type": "string"
},
"id": {
"type": "integer"
},
"image_url": {
"type": "string"
},
"name": {
"type": "string"
},
"url": {
"type": "string"
}
},
"type": "object"
},
"portfolio_api_types_User": {
"properties": {
"-": {
"type": "string"
},
"email": {
"type": "string"
},
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"role": {
"type": "string"
}
},
"required": [
"-"
],
"type": "object"
}
}
},
"info": { "info": {
"title": "portfolio", "title": "portfolio",
"version": "0.0.0" "version": "0.0.0"
}, },
"openapi": "3.0.0", "openapi": "3.0.0",
"paths": { "paths": {
"/nfc/{uid}": { "/check": {
"get": { "get": {
"description": "Get nfc data by uid.", "description": "check for user jwt cookie",
"parameters": [ "responses": {
{ "200": {
"description": "id of the user", "content": {
"in": "path", "application/json": {
"name": "uid",
"required": true,
"schema": { "schema": {
"pattern": "\\d+",
"type": "string" "type": "string"
} }
} }
], },
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/htmx/canEdit": {
"get": {
"description": "check if user is allowed to edit",
"responses": { "responses": {
"200": { "200": {
"content": { "content": {
@ -37,6 +144,453 @@
} }
} }
} }
},
"/login": {
"post": {
"description": "Login.",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/portfolio_api_types_LoginUser"
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"401": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/project": {
"post": {
"description": "Create project",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"401": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/project/{id}": {
"get": {
"description": "Get project by id",
"parameters": [
{
"description": "id of the project",
"in": "path",
"name": "id",
"required": true,
"schema": {
"pattern": "\\d+",
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/portfolio_api_types_Project"
}
}
},
"description": ""
},
"400": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
},
"patch": {
"description": "Update project by id",
"parameters": [
{
"description": "id of the project",
"in": "path",
"name": "id",
"required": true,
"schema": {
"pattern": "\\d+",
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/portfolio_api_types_Project"
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"400": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"401": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/projects": {
"get": {
"description": "Get projects",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/portfolio_api_types_Project"
},
"nullable": true,
"type": "array"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
},
"patch": {
"description": "Update projects WIP",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/portfolio_api_types_Project"
},
"nullable": true,
"type": "array"
}
}
},
"description": ""
},
"401": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/register": {
"post": {
"description": "Register.",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/portfolio_api_types_User"
}
}
}
},
"responses": {
"201": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"400": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
},
"/user/{uid}": {
"get": {
"description": "Get user by uid.",
"parameters": [
{
"description": "id of the user",
"in": "path",
"name": "id",
"required": true,
"schema": {
"pattern": "\\d+",
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/portfolio_api_types_User"
}
}
},
"description": ""
},
"400": {
"content": {
"application/json": {
"schema": {
"type": "string"
}
}
},
"description": ""
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"500": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": ""
},
"default": {
"description": ""
}
}
}
} }
} }
} }