Dima 2 rokov pred
rodič
commit
3a6418a52f

BIN
build/app/app


+ 2 - 0
internal/interfaces/repository.go

@@ -14,6 +14,7 @@ type IArticleRepository interface {
 	GetByID(ctx context.Context, ID int) (*models.Article, error)
 	Add(ctx context.Context, m models.Article) (int, error)
 	Update(ctx context.Context, m models.Article) error
+	Delete(ctx context.Context, ID int) error
 }
 
 type ITagRepository interface {
@@ -31,4 +32,5 @@ type ITagRepository interface {
 type IArticleTagRepository interface {
 	Add(ctx context.Context, articleID int, tagIDs []int) error
 	Delete(ctx context.Context, articleID int, tagIDs []int) error
+	DeleteByArticleID(ctx context.Context, articleID int) error
 }

+ 8 - 0
internal/repositories/article.go

@@ -187,3 +187,11 @@ func (a articleRepository) Update(ctx context.Context, m models.Article) error {
 
 	return err
 }
+
+func (a articleRepository) Delete(ctx context.Context, id int) error {
+	query := "DELETE FROM " + articleTableName + " WHERE id = ?"
+
+	_, err := a.db.ExecContext(ctx, query, id)
+
+	return err
+}

+ 8 - 0
internal/repositories/article_tag.go

@@ -53,3 +53,11 @@ func (a articleTagRepository) Delete(ctx context.Context, articleID int, tagIDs
 
 	return err
 }
+
+func (a articleTagRepository) DeleteByArticleID(ctx context.Context, articleID int) error {
+	query := "DELETE FROM " + articleTagTableName + " WHERE article_id = ?"
+
+	_, err := a.db.ExecContext(ctx, query, articleID)
+
+	return err
+}

+ 32 - 1
internal/services/handler/admin/article.go

@@ -249,6 +249,37 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
 
 func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
-		return nil
+		ID, err := strconv.Atoi(ctx.Params("id"))
+		if err != nil {
+			return err
+		}
+
+		article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
+		if err != nil {
+			return err
+		}
+
+		if ctx.Method() == fiber.MethodPost {
+			err = app.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID)
+			if err != nil {
+				return err
+			}
+
+			err = app.GetArticleRepository().Delete(ctx.Context(), ID)
+			if err != nil {
+				return err
+			}
+
+			app.GetCacheService().FlushAll()
+
+			if err = ctx.Redirect("/admin"); err != nil {
+				return err
+			}
+		}
+
+		return ctx.Render("admin/article_delete", fiber.Map{
+			"article": article.Title,
+			"section": "article",
+		}, "admin/_layout")
 	}
 }

+ 12 - 0
internal/templates/admin/article_delete.html

@@ -0,0 +1,12 @@
+<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
+    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2">
+        <h3 class="h3">Удаление статьи</h3>
+    </div>
+    <div class="mb-3 border-bottom border-top pb-3 pt-3">
+        Подтвердите удаление статьи <b>{{ .article }}</b>
+    </div>
+    <form method="post">
+        <a href="/admin" class="btn btn-sm btn-secondary">Назад</a>
+        <button type="submit" class="btn btn-sm btn-danger">Удалить</button>
+    </form>
+</main>