chore: init

This commit is contained in:
DevMiner 2024-08-11 03:51:22 +02:00
commit 320715f3e7
174 changed files with 42083 additions and 0 deletions

View file

@ -0,0 +1,32 @@
package note_handler
import (
"github.com/gofiber/fiber/v2"
"github.com/lysand-org/versia-go/internal/api_schema"
)
func (i *Handler) CreateNote(c *fiber.Ctx) error {
req := api_schema.CreateNoteRequest{}
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "invalid request",
})
}
if err := i.bodyValidator.Validate(req); err != nil {
return err
}
n, err := i.noteService.CreateNote(c.UserContext(), req)
if err != nil {
return err
}
if n == nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "failed to create note",
})
}
return c.Status(fiber.StatusCreated).JSON(api_schema.Note{
ID: n.ID,
})
}

View file

@ -0,0 +1,28 @@
package note_handler
import (
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/api_schema"
)
func (i *Handler) GetNote(c *fiber.Ctx) error {
parsedRequestedNoteID, err := uuid.Parse(c.Params("id"))
if err != nil {
return api_schema.ErrBadRequest(map[string]any{
"reason": "Invalid note ID",
})
}
u, err := i.noteService.GetNote(c.UserContext(), parsedRequestedNoteID)
if err != nil {
i.log.Error(err, "Failed to query note", "id", parsedRequestedNoteID)
return api_schema.ErrInternalServerError(map[string]any{"reason": "Failed to query note"})
}
if u == nil {
return api_schema.ErrNotFound(nil)
}
return c.JSON(u.ToLysand())
}

View file

@ -0,0 +1,35 @@
package note_handler
import (
"github.com/go-logr/logr"
"github.com/gofiber/fiber/v2"
"github.com/lysand-org/versia-go/config"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/internal/validators"
"github.com/lysand-org/versia-go/pkg/webfinger"
)
type Handler struct {
noteService service.NoteService
bodyValidator validators.BodyValidator
hostMeta webfinger.HostMeta
log logr.Logger
}
func New(noteService service.NoteService, bodyValidator validators.BodyValidator, log logr.Logger) *Handler {
return &Handler{
noteService: noteService,
bodyValidator: bodyValidator,
hostMeta: webfinger.NewHostMeta(config.C.PublicAddress),
log: log.WithName("users"),
}
}
func (i *Handler) Register(r fiber.Router) {
r.Get("/api/app/notes/:id", i.GetNote)
r.Post("/api/app/notes/", i.CreateNote)
}