Docs update

This commit is contained in:
darius 2024-07-04 12:32:48 +02:00
parent 9925d218ff
commit 7dc76a674e
12 changed files with 144 additions and 14 deletions

View File

@ -2,7 +2,10 @@ module openAPI
go 1.22
require github.com/a-h/rest v0.0.0-20240504113546-6729b3328f85
require (
github.com/a-h/respond v0.0.2
github.com/a-h/rest v0.0.0-20240504113546-6729b3328f85
)
require (
github.com/getkin/kin-openapi v0.124.0 // indirect

View File

@ -1,3 +1,5 @@
github.com/a-h/respond v0.0.2 h1:mhBwB2XuM+34gfIFs9LuXGfCCbu00rvaCWpTVNHvkPU=
github.com/a-h/respond v0.0.2/go.mod h1:k9UvuVDWmHAb91OsdrqG0xFv7X+HelBpfMJIn9xMYWM=
github.com/a-h/rest v0.0.0-20240504113546-6729b3328f85 h1:Lj+OmK3+dKMuR8OdlnUeIrgdt8JkcNlA9isS2Aey5Mg=
github.com/a-h/rest v0.0.0-20240504113546-6729b3328f85/go.mod h1:5wH1imbpKnMjll8xpGRdg0Sb0HwH7nYiM5VPm0Zl5Bw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"github.com/a-h/rest"
"log"
"net/http"
"os"
)
@ -15,13 +14,12 @@ func main() {
api = rest.NewAPI("portfolio")
api.StripPkgPaths = []string{"github.com/a-h/rest/example", "github.com/a-h/respond"}
api.Get("/nfc/{uid}").
HasPathParameter("uid", rest.PathParam{
Description: "id of the user",
Regexp: `\d+`,
}).
HasDescription("Get nfc data by uid.").
HasResponseModel(http.StatusOK, rest.ModelOf[string]())
// register models
RegisterModels()
// register endpoints
RegisterGenericEndpoints()
RegisterUserEndpoints()
RegisterProjectEndpoints()
// Create the specification.
spec, err := api.Spec()

View File

@ -0,0 +1,20 @@
package main
import (
"github.com/a-h/respond"
"github.com/a-h/rest"
"net/http"
)
func RegisterGenericEndpoints() {
api.Get("/check").
HasDescription("check for user jwt cookie").
HasResponseModel(http.StatusOK, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Get("/htmx/canEdit").
HasDescription("check if user is allowed to edit").
HasResponseModel(http.StatusOK, rest.ModelOf[string]())
}

View File

@ -0,0 +1,9 @@
package main
// ############## MUST DO ########################
// local copy of types to fix unintended behaviour
// ###############################################
func RegisterModels() {
}

View File

@ -0,0 +1,52 @@
package main
import (
"github.com/a-h/respond"
"github.com/a-h/rest"
"net/http"
"portfolio/database/ent"
)
func RegisterProjectEndpoints() {
api.Post("/project").
HasDescription("Create project").
HasResponseModel(http.StatusOK, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]())
api.Patch("/project/{id}").
HasPathParameter("id", rest.PathParam{
Description: "id of the project",
Regexp: `\d+`,
}).
HasDescription("Update project by id").
HasRequestModel(rest.ModelOf[ent.Project]()).
HasResponseModel(http.StatusOK, rest.ModelOf[string]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]())
api.Patch("/projects").
HasDescription("Update projects WIP").
HasResponseModel(http.StatusOK, rest.ModelOf[[]ent.Project]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]())
api.Get("/project/{id}").
HasPathParameter("id", rest.PathParam{
Description: "id of the project",
Regexp: `\d+`,
}).
HasDescription("Get project by id").
HasResponseModel(http.StatusOK, rest.ModelOf[ent.Project]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Get("/projects").
HasDescription("Get projects").
HasResponseModel(http.StatusOK, rest.ModelOf[[]ent.Project]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
}

View File

@ -0,0 +1,39 @@
package main
import (
"github.com/a-h/respond"
"github.com/a-h/rest"
"net/http"
"portfolio/api/types"
"portfolio/database/ent"
)
func RegisterUserEndpoints() {
api.Get("/user/{uid}").
HasPathParameter("id", rest.PathParam{
Description: "id of the user",
Regexp: `\d+`,
}).
HasDescription("Get user by uid.").
HasResponseModel(http.StatusOK, rest.ModelOf[ent.User]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Post("/register").
HasDescription("Register.").
HasRequestModel(rest.ModelOf[ent.User]()).
HasResponseModel(http.StatusCreated, rest.ModelOf[string]()).
HasResponseModel(http.StatusBadRequest, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]())
api.Post("/login").
HasDescription("Login.").
HasRequestModel(rest.ModelOf[types.LoginUser]()).
HasResponseModel(http.StatusOK, rest.ModelOf[string]()).
HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnprocessableEntity, rest.ModelOf[respond.Error]()).
HasResponseModel(http.StatusUnauthorized, rest.ModelOf[string]())
}

View File

@ -35,7 +35,7 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {
}
func UpdateProjectHandler(w http.ResponseWriter, r *http.Request) {
//todo htmx check
_, _, err := jwt.VerifyUser(r)
if err != nil {
UnauthorizedHandler(w)

View File

@ -57,6 +57,7 @@ func GetUserHandler(w http.ResponseWriter, r *http.Request) {
User, err := query.GetUser(context.Background(), userID)
if err != nil {
UnprocessableEntityHandler(w, err)
return
}
@ -64,6 +65,7 @@ func GetUserHandler(w http.ResponseWriter, r *http.Request) {
err = json.NewEncoder(w).Encode(User)
if err != nil {
InternalServerErrorHandler(w, err)
return
}
}

View File

@ -3,3 +3,8 @@ package types
type Username struct {
Name string
}
type LoginUser struct {
Email string
Password string
}

View File

@ -18,7 +18,8 @@ func (User) Fields() []ent.Field {
Unique(),
field.String("email").
Unique(),
field.String("password"),
field.String("password").
Sensitive(),
field.Enum("role").
Values("owner", "admin", "user", "visitor"),
}

View File

@ -21,7 +21,7 @@ type User struct {
// Email holds the value of the "email" field.
Email string `json:"email,omitempty"`
// Password holds the value of the "password" field.
Password string `json:"password,omitempty"`
Password string `json:"-"`
// Role holds the value of the "role" field.
Role user.Role `json:"role,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
@ -165,8 +165,7 @@ func (u *User) String() string {
builder.WriteString("email=")
builder.WriteString(u.Email)
builder.WriteString(", ")
builder.WriteString("password=")
builder.WriteString(u.Password)
builder.WriteString("password=<sensitive>")
builder.WriteString(", ")
builder.WriteString("role=")
builder.WriteString(fmt.Sprintf("%v", u.Role))