mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: post edition save
This commit is contained in:
@@ -31,9 +31,7 @@ func (a *PostRepository) Get(id uint32) (domain.Post, error) {
|
||||
|
||||
func (a *PostRepository) GetByName(name string) (domain.Post, error) {
|
||||
var post entity.Post
|
||||
err := a.db.Model(&entity.Post{
|
||||
Title: name,
|
||||
}).First(&post).Error
|
||||
err := a.db.Model(&entity.Post{}).Where("title = ?", name).First(&post).Error
|
||||
if err != nil {
|
||||
return domain.Post{}, err
|
||||
}
|
||||
@@ -71,4 +69,25 @@ func (a *PostRepository) GetAll() []domain.Post {
|
||||
return domainPosts
|
||||
}
|
||||
|
||||
func (a *PostRepository) UpdateBody(id uint32, body string) error {
|
||||
var localPost entity.Post
|
||||
err := a.db.Model(&entity.Post{}).First(&localPost, id).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
localPost.Body = body
|
||||
err = a.db.Save(&localPost).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *PostRepository) Delete(id uint32) error {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
var _ gateways.IPostRepository = &PostRepository{}
|
||||
|
||||
@@ -6,28 +6,36 @@
|
||||
</head>
|
||||
<body class="text-dark vh-100 d-flex flex-column">
|
||||
{{.Navbar}}
|
||||
<div class="container mt-3 text-black-50 d-flex flex-column h-100">
|
||||
<form class="container mt-3 text-black-50 d-flex flex-column h-100" action="edit" method="post">
|
||||
<div class="d-flex align-items-center justify-content-between">
|
||||
<div>
|
||||
<h1>Post - edition</h1>
|
||||
<label for="postContent">Edition</label>
|
||||
<label for="postBody">Edition</label>
|
||||
</div>
|
||||
{{ if .Alert.Message }}
|
||||
<div class="alert {{ if .Alert.IsError }} alert-danger {{ else }} alert-success {{ end }} alert-dismissible" role="alert">
|
||||
{{ .Alert.Message }}
|
||||
<button type="button" class="btn-close" aria-label="Close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
{{ end }}
|
||||
<div class="d-flex justify-content-center align-items-center">
|
||||
<button class="btn btn-success d-flex justify-content-center align-items-center gap-1" style="fill: white;">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24"><path d="M840-680v480q0 33-23.5 56.5T760-120H200q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h480l160 160Zm-80 34L646-760H200v560h560v-446ZM480-240q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35ZM240-560h360v-160H240v160Zm-40-86v446-560 114Z"/></svg>
|
||||
<button class="btn btn-success d-flex justify-content-center align-items-center gap-1" style="fill: white;" type="submit">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24">
|
||||
<path d="M840-680v480q0 33-23.5 56.5T760-120H200q-33 0-56.5-23.5T120-200v-560q0-33 23.5-56.5T200-840h480l160 160Zm-80 34L646-760H200v560h560v-446ZM480-240q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35ZM240-560h360v-160H240v160Zm-40-86v446-560 114Z"/>
|
||||
</svg>
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-black-50 d-flex align-items-center justify-content-center h-100 py-3">
|
||||
<textarea id="postContent" name="postContent">{{.Body}}</textarea>
|
||||
<textarea id="postBody" name="postBody">{{.Body}}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<script src="/static/tinymce/js/tinymce/tinymce.min.js"></script>
|
||||
<script>
|
||||
tinymce.init({
|
||||
selector: '#postContent',
|
||||
selector: '#postBody',
|
||||
promotion: false,
|
||||
plugins: 'image',
|
||||
width: '100%',
|
||||
|
||||
@@ -14,6 +14,7 @@ type UseCases struct {
|
||||
CreatePostUseCase *useCases.CreatePostUseCase
|
||||
GetPostUseCase *useCases.GetPostUseCase
|
||||
ListPostsUseCase *useCases.ListPostsUseCase
|
||||
UpdatePostUseCase *useCases.UpdatePostUseCase
|
||||
GetUserUseCase *useCases.GetUserUseCase
|
||||
CreateUserUseCase *useCases.CreateUserUseCase
|
||||
UpdateUserUseCase *useCases.UpdateUserUseCase
|
||||
@@ -49,6 +50,7 @@ func InitContainer() {
|
||||
CreatePostUseCase: useCases.NewCreatePostUseCase(db),
|
||||
GetPostUseCase: useCases.NewGetPostUseCase(db),
|
||||
ListPostsUseCase: useCases.NewListPostsUseCase(db),
|
||||
UpdatePostUseCase: useCases.NewUpdatePostUseCase(db),
|
||||
GetUserUseCase: useCases.NewGetUserUseCase(db),
|
||||
CreateUserUseCase: useCases.NewCreateUserUseCase(db),
|
||||
UpdateUserUseCase: useCases.NewUpdateUserUseCase(db),
|
||||
|
||||
@@ -123,6 +123,7 @@ func NewPageRouter() http.Handler {
|
||||
r.Get("/home", GetHomePage)
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/post/{name}/edit", GetPostEditPage)
|
||||
r.Post("/post/{name}/edit", PostPostEditPage)
|
||||
r.Get("/post/create", GetPostCreatePage)
|
||||
r.Post("/post/create", PostPostCreatePage)
|
||||
})
|
||||
|
||||
+53
-10
@@ -4,22 +4,65 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type PostEditPageAlert struct {
|
||||
IsError bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func getPostEditPageTemplate(body string, alert PostEditPageAlert) []byte {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postTmpl, _ := Container.GetPageUseCase.GetPage("postEdit", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Body": body,
|
||||
"Alert": alert,
|
||||
})
|
||||
return postTmpl
|
||||
}
|
||||
|
||||
func PostPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postName := chi.URLParam(r, "name")
|
||||
if len(postName) == 0 {
|
||||
_, _ = w.Write(getPostEditPageTemplate("", PostEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not find the requested post.",
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_ = r.ParseForm()
|
||||
postBody := r.FormValue("postBody")
|
||||
|
||||
parsedName, _ := url.PathUnescape(postName)
|
||||
post, _ := Container.GetPostUseCase.GetPostByName(parsedName)
|
||||
err := Container.UpdatePostUseCase.UpdateBody(post.ID, postBody)
|
||||
if err != nil {
|
||||
_, _ = w.Write(getPostEditPageTemplate(postBody, PostEditPageAlert{
|
||||
IsError: true,
|
||||
Message: "Could not save the post: " + err.Error(),
|
||||
}))
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = w.Write(getPostEditPageTemplate(postBody, PostEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "Post successfully edited!",
|
||||
}))
|
||||
}
|
||||
|
||||
func GetPostEditPage(w http.ResponseWriter, r *http.Request) {
|
||||
postName := chi.URLParam(r, "name")
|
||||
if len(postName) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
post, _ := Container.GetPostUseCase.GetPostByName(postName)
|
||||
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postsTmpl, _ := Container.GetPageUseCase.GetPage("postEdit", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
"Body": post.Body,
|
||||
})
|
||||
_, _ = w.Write(postsTmpl)
|
||||
parsedName, _ := url.PathUnescape(postName)
|
||||
post, _ := Container.GetPostUseCase.GetPostByName(parsedName)
|
||||
_, _ = w.Write(getPostEditPageTemplate(post.Body, PostEditPageAlert{
|
||||
IsError: false,
|
||||
Message: "",
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -9,4 +9,6 @@ type IPostRepository interface {
|
||||
GetByName(name string) (post.Post, error)
|
||||
GetAll() []post.Post
|
||||
Create(post post.Post) (post.Post, error)
|
||||
UpdateBody(id uint32, body string) error
|
||||
Delete(id uint32) error
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package useCases
|
||||
|
||||
import (
|
||||
"GoCMS/adapters/secondary/gateways"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UpdatePostUseCase struct {
|
||||
postRepository gateways.PostRepository
|
||||
}
|
||||
|
||||
func NewUpdatePostUseCase(db *gorm.DB) *UpdatePostUseCase {
|
||||
return &UpdatePostUseCase{
|
||||
postRepository: *gateways.NewPostRepository(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *UpdatePostUseCase) UpdateBody(id uint32, body string) error {
|
||||
return g.postRepository.UpdateBody(id, body)
|
||||
}
|
||||
@@ -10,10 +10,6 @@ type UpdateUserUseCase struct {
|
||||
userRepository gateways.UserRepository
|
||||
}
|
||||
|
||||
type UpdateVerificationStatusCommand struct {
|
||||
isVerified bool
|
||||
}
|
||||
|
||||
func NewUpdateUserUseCase(db *gorm.DB) *UpdateUserUseCase {
|
||||
return &UpdateUserUseCase{
|
||||
userRepository: *gateways.NewUserRepository(db),
|
||||
|
||||
Reference in New Issue
Block a user