Skip to content

Commit eff9f16

Browse files
committed
add book+note creation
1 parent 8374559 commit eff9f16

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

backend/db/types.go

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package db
22

3+
import "github.com/google/uuid"
4+
35
type CreateUser struct {
46
Username string `json:"username" validate:"required,alphanum,min=3,max=30"`
57
Password string `json:"password" validate:"required"`
@@ -12,3 +14,32 @@ func (u *CreateUser) IntoUser() User {
1214
user.SetPassword(u.Password)
1315
return user
1416
}
17+
18+
type CreateBook struct {
19+
Name string `json:"name" validate:"required"`
20+
Slug string `json:"slug" validate:"required,slug"`
21+
IsPublic bool `json:"isPublic"`
22+
}
23+
24+
func (b *CreateBook) IntoBook(ownerID uuid.UUID) Book {
25+
return Book{
26+
Name: b.Name,
27+
Slug: b.Slug,
28+
OwnerID: ownerID,
29+
IsPublic: b.IsPublic,
30+
}
31+
}
32+
33+
type CreateNote struct {
34+
Name string `json:"name" validate:"required"`
35+
Slug string `json:"slug" validate:"required,slug"`
36+
BookID uuid.UUID `json:"bookId" validate:"required"`
37+
}
38+
39+
func (n *CreateNote) IntoNote() Note {
40+
return Note{
41+
Name: n.Name,
42+
Slug: n.Slug,
43+
BookID: n.BookID,
44+
}
45+
}

backend/routes/books.go

+16
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@ package routes
33
import (
44
"net/http"
55

6+
"github.com/enchant97/note-mark/backend/core"
67
"github.com/enchant97/note-mark/backend/db"
78
"github.com/google/uuid"
89
"github.com/labstack/echo/v4"
910
)
1011

12+
func createBook(ctx echo.Context) error {
13+
authenticatedUser := getAuthenticatedUser(ctx)
14+
var bookData db.CreateBook
15+
if err := core.BindAndValidate(ctx, bookData); err != nil {
16+
return err
17+
}
18+
19+
book := bookData.IntoBook(authenticatedUser.UserID)
20+
if err := db.DB.Create(&book).Error; err != nil {
21+
return err
22+
}
23+
24+
return ctx.JSON(http.StatusCreated, book)
25+
}
26+
1127
func getBooksByUsername(ctx echo.Context) error {
1228
authenticatedUser := getAuthenticatedUser(ctx)
1329
username := ctx.Param("username")

backend/routes/notes.go

+23
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@ package routes
33
import (
44
"net/http"
55

6+
"github.com/enchant97/note-mark/backend/core"
67
"github.com/enchant97/note-mark/backend/db"
78
"github.com/google/uuid"
89
"github.com/labstack/echo/v4"
910
)
1011

12+
func createNote(ctx echo.Context) error {
13+
authenticatedUser := getAuthenticatedUser(ctx)
14+
var noteData db.CreateNote
15+
if err := core.BindAndValidate(ctx, noteData); err != nil {
16+
return err
17+
}
18+
19+
// TODO can this be made more effient?
20+
if err := db.DB.
21+
First(&db.Book{}, "owner_id = ?", authenticatedUser.UserID).
22+
Error; err != nil {
23+
return err
24+
}
25+
26+
note := noteData.IntoNote()
27+
if err := db.DB.Create(&note).Error; err != nil {
28+
return err
29+
}
30+
31+
return ctx.JSON(http.StatusCreated, note)
32+
}
33+
1134
func getNotesByBookID(ctx echo.Context) error {
1235
authenticatedUser := getAuthenticatedUser(ctx)
1336
bookID, err := uuid.Parse(ctx.Param("bookID"))

backend/routes/utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ func InitRoutes(e *echo.Echo, appConfig config.AppConfig) {
5757
slugUserRoutes.GET("books/:bookSlug/notes/", getNotesBySlug)
5858
slugUserRoutes.GET("books/:bookSlug/notes/:noteSlug/", getNoteBySlug)
5959
}
60+
protectedRoutes.POST("books/", createBook)
6061
protectedRoutes.GET("books/:bookID", getBookByID)
6162
protectedRoutes.GET("books/:bookID/notes/", getNotesByBookID)
63+
protectedRoutes.POST("notes/", createNote)
6264
protectedRoutes.GET("notes/:noteID/", getNoteByID)
6365
}
6466
}

0 commit comments

Comments
 (0)