diff --git a/cmd/api/blogs.go b/cmd/api/blogs.go index aa71368..79f6284 100644 --- a/cmd/api/blogs.go +++ b/cmd/api/blogs.go @@ -64,6 +64,8 @@ func (app *application) showBlogHandler(w http.ResponseWriter, r *http.Request) func (app *application) createBlogHandler(w http.ResponseWriter, r *http.Request) { r.ParseMultipartForm(data.DefaultMaxMemory) + user := app.contextGetUser(r) + file, handler, err := r.FormFile("thumbnail") if err != nil { app.fileNotFoundResponse(w, r, "thumbnail") @@ -84,7 +86,7 @@ func (app *application) createBlogHandler(w http.ResponseWriter, r *http.Request } blog := &data.Blog{ - BlogCategoryID: blogCategoryId, + BlogCategoryID: int64(blogCategoryId), Thumbnail: url, Title: r.FormValue("title"), Description: r.FormValue("description"), @@ -95,7 +97,7 @@ func (app *application) createBlogHandler(w http.ResponseWriter, r *http.Request Feature: r.FormValue("feature") == "true", Status: r.FormValue("status"), Tags: r.FormValue("tags"), - CreatedBy: 1, + CreatedBy: user.ID, CreatedByText: "Admin", } @@ -167,7 +169,7 @@ func (app *application) updateBlogHandler(w http.ResponseWriter, r *http.Request } blog.Thumbnail = url - blog.BlogCategoryID = blogCategoryId + blog.BlogCategoryID = int64(blogCategoryId) blog.Title = r.FormValue("title") blog.Description = r.FormValue("description") blog.Content = r.FormValue("content") diff --git a/cmd/api/routes.go b/cmd/api/routes.go index fdd458a..6aab808 100644 --- a/cmd/api/routes.go +++ b/cmd/api/routes.go @@ -119,7 +119,7 @@ func (app *application) routes() http.Handler { // Blogs router.HandlerFunc(http.MethodGet, "/cms/blogs", app.listBlogsHandler) router.HandlerFunc(http.MethodGet, "/cms/blogs/:id", app.showBlogHandler) - router.HandlerFunc(http.MethodPost, "/cms/blogs", app.createBlogHandler) + router.HandlerFunc(http.MethodPost, "/cms/blogs", app.requireAuthenticatedAdmin(app.createBlogHandler)) router.HandlerFunc(http.MethodPut, "/cms/blogs/:id", app.updateBlogHandler) router.HandlerFunc(http.MethodDelete, "/cms/blogs/:id", app.deleteBlogHandler) diff --git a/internal/data/blogs.go b/internal/data/blogs.go index 3115794..a6f16a2 100644 --- a/internal/data/blogs.go +++ b/internal/data/blogs.go @@ -12,7 +12,7 @@ import ( type Blog struct { ID int64 `json:"id"` BlogCategory BlogCategory `json:"blog_category"` - BlogCategoryID int `json:"blog_category_id"` + BlogCategoryID int64 `json:"blog_category_id"` Thumbnail string `json:"thumbnail"` Title string `json:"title"` Description string `json:"description"` @@ -23,7 +23,7 @@ type Blog struct { Feature bool `json:"feature"` Status string `json:"status"` Tags string `json:"tags"` - CreatedBy int `json:"created_by"` + CreatedBy int64 `json:"created_by"` DeletedAt time.Time `json:"-"` CreatedAt time.Time `json:"-"` UpdatedAt time.Time `json:"-"`