File tree 4 files changed +72
-0
lines changed
4 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1
1
package db
2
2
3
+ import "github.com/google/uuid"
4
+
3
5
type CreateUser struct {
4
6
Username string `json:"username" validate:"required,alphanum,min=3,max=30"`
5
7
Password string `json:"password" validate:"required"`
@@ -12,3 +14,32 @@ func (u *CreateUser) IntoUser() User {
12
14
user .SetPassword (u .Password )
13
15
return user
14
16
}
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
+ }
Original file line number Diff line number Diff line change @@ -3,11 +3,27 @@ package routes
3
3
import (
4
4
"net/http"
5
5
6
+ "github.com/enchant97/note-mark/backend/core"
6
7
"github.com/enchant97/note-mark/backend/db"
7
8
"github.com/google/uuid"
8
9
"github.com/labstack/echo/v4"
9
10
)
10
11
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
+
11
27
func getBooksByUsername (ctx echo.Context ) error {
12
28
authenticatedUser := getAuthenticatedUser (ctx )
13
29
username := ctx .Param ("username" )
Original file line number Diff line number Diff line change @@ -3,11 +3,34 @@ package routes
3
3
import (
4
4
"net/http"
5
5
6
+ "github.com/enchant97/note-mark/backend/core"
6
7
"github.com/enchant97/note-mark/backend/db"
7
8
"github.com/google/uuid"
8
9
"github.com/labstack/echo/v4"
9
10
)
10
11
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
+
11
34
func getNotesByBookID (ctx echo.Context ) error {
12
35
authenticatedUser := getAuthenticatedUser (ctx )
13
36
bookID , err := uuid .Parse (ctx .Param ("bookID" ))
Original file line number Diff line number Diff line change @@ -57,8 +57,10 @@ func InitRoutes(e *echo.Echo, appConfig config.AppConfig) {
57
57
slugUserRoutes .GET ("books/:bookSlug/notes/" , getNotesBySlug )
58
58
slugUserRoutes .GET ("books/:bookSlug/notes/:noteSlug/" , getNoteBySlug )
59
59
}
60
+ protectedRoutes .POST ("books/" , createBook )
60
61
protectedRoutes .GET ("books/:bookID" , getBookByID )
61
62
protectedRoutes .GET ("books/:bookID/notes/" , getNotesByBookID )
63
+ protectedRoutes .POST ("notes/" , createNote )
62
64
protectedRoutes .GET ("notes/:noteID/" , getNoteByID )
63
65
}
64
66
}
You can’t perform that action at this time.
0 commit comments