Bladeren bron

Update admin article page

Dima 2 jaren geleden
bovenliggende
commit
0e92f01d68

+ 5 - 1
internal/helpers/trans.go

@@ -10,7 +10,11 @@ import (
 )
 
 var fieldNames = map[string]string{
-	"Tag": "Тег",
+	"Tag":         "Тег",
+	"Title":       "Название",
+	"Image":       "Картинка",
+	"PublishTime": "Дата публикации",
+	"Text":        "Текст",
 }
 
 func GetDefaultTranslator(v *validator.Validate) (ut.Translator, error) {

+ 1 - 0
internal/interfaces/repository.go

@@ -7,6 +7,7 @@ import (
 )
 
 type IArticleRepository interface {
+	GetAll(ctx context.Context) ([]models.Article, error)
 	GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error)
 	GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error)
 	GetByURL(ctx context.Context, url string) (*models.Article, error)

+ 24 - 0
internal/mapper/article.go

@@ -6,6 +6,30 @@ import (
 	"github.com/dmitriygnatenko/internal/models"
 )
 
+func ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) {
+	res := make([]dto.ArticleDTO, 0, len(m))
+
+	for i := range m {
+		parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
+		if err != nil {
+			return nil, err
+		}
+
+		res = append(res, dto.ArticleDTO{
+			ID:              m[i].ID,
+			URL:             m[i].URL,
+			Title:           m[i].Title,
+			Text:            m[i].Text,
+			PublishTime:     helpers.FormatDateStr(parsedDateTime),
+			Image:           m[i].Image.String,
+			MetaKeywords:    m[i].MetaKeywords.String,
+			MetaDescription: m[i].MetaDescription.String,
+		})
+	}
+
+	return res, nil
+}
+
 func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
 	parsedDateTime, err := helpers.ParseDateTime(m.PublishTime)
 	if err != nil {

+ 31 - 0
internal/repositories/article.go

@@ -55,6 +55,37 @@ func (a articleRepository) GetAllPreview(ctx context.Context) ([]models.ArticleP
 	return res, nil
 }
 
+func (a articleRepository) GetAll(ctx context.Context) ([]models.Article, error) {
+	var res []models.Article
+
+	query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
+		"FROM " + articleTableName + " " +
+		"ORDER BY publish_time DESC"
+
+	rows, err := a.db.QueryContext(ctx, query)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+
+	for rows.Next() {
+		row := models.Article{}
+
+		err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.Image, &row.Text, &row.PreviewText, &row.MetaKeywords, &row.MetaDescription, &row.IsActive)
+		if err != nil {
+			return nil, err
+		}
+
+		res = append(res, row)
+	}
+
+	if err = rows.Err(); err != nil {
+		return nil, err
+	}
+
+	return res, nil
+}
+
 func (a articleRepository) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
 	var res []models.ArticlePreview
 

+ 2 - 2
internal/services/handler/admin/article.go

@@ -12,12 +12,12 @@ import (
 
 func ArticleHandler(app interfaces.IApp) fiber.Handler {
 	return func(ctx *fiber.Ctx) error {
-		articles, err := app.GetArticleRepository().GetAllPreview(ctx.Context())
+		articles, err := app.GetArticleRepository().GetAll(ctx.Context())
 		if err != nil {
 			return err
 		}
 
-		articlesDTO, err := mapper.ConvertArticlePreviewModelsToDTO(articles)
+		articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles)
 		if err != nil {
 			return err
 		}

+ 1 - 1
internal/templates/admin/article.html

@@ -1,6 +1,6 @@
 <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">
-        <h2 class="h2">Статьи</h2>
+        <h3 class="h3">Статьи</h3>
         <div class="btn-toolbar mb-2 mb-md-0">
             <a class="btn btn-sm btn-primary" href="/admin/article/add" title="Добавить">
                 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-circle" viewBox="0 0 16 16">

+ 26 - 21
internal/templates/admin/article_edit.html

@@ -21,22 +21,27 @@
 {{ end }}
 <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 border-bottom">
-        <h2 class="h2">{{ .title }}</h2>
+        <h3 class="h3">{{ .title }}</h3>
     </div>
     <form method="post" class="mb-5">
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Статья активна</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Статья активна</label>
             <div class="col-sm-10">
                 <div class="form-check form-switch pt-2">
                     <input class="form-check-input" type="checkbox" name="is_active" {{ if .form.IsActive }}checked{{ end }}>
                 </div>
             </div>
         </div>
+
+
+
+
+
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Название</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Название</label>
             <div class="col-sm-10">
                 {{ $titleErr := index .errors "ArticleForm.Title" }}
-                <input type="text" class="form-control {{ if $titleErr }}is-invalid{{ end }}" name="title" value="{{ .form.Title }}">
+                <input type="text" class="form-control form-control-sm {{ if $titleErr }}is-invalid{{ end }}" name="title" value="{{ .form.Title }}">
                 {{ if $titleErr }}
                 <div class="invalid-feedback">
                     {{ $titleErr }}
@@ -45,10 +50,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Картинка</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Картинка</label>
             <div class="col-sm-10">
                 {{ $imageErr := index .errors "ArticleForm.Image" }}
-                <input type="text" class="form-control {{ if $imageErr }}is-invalid{{ end }}" name="title" value="{{ .form.Image }}">
+                <input type="text" class="form-control form-control-sm {{ if $imageErr }}is-invalid{{ end }}" name="title" value="{{ .form.Image }}">
                 {{ if $imageErr }}
                 <div class="invalid-feedback">
                     {{ $imageErr }}
@@ -57,10 +62,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">URL</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">URL</label>
             <div class="col-sm-10">
                 {{ $urlErr := index .errors "ArticleForm.URL" }}
-                <input type="text" class="form-control {{ if $urlErr }}is-invalid{{ end }}" name="url" value="{{ .form.URL }}">
+                <input type="text" class="form-control form-control-sm {{ if $urlErr }}is-invalid{{ end }}" name="url" value="{{ .form.URL }}">
                 {{ if $urlErr }}
                 <div class="invalid-feedback">
                     {{ $urlErr }}
@@ -69,10 +74,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Дата публикации</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Дата публикации</label>
             <div class="col-sm-10">
                 {{ $dateErr := index .errors "ArticleForm.PublishTime" }}
-                <input type="date" class="form-control col-auto {{ if $dateErr }}is-invalid{{ end }}" name="publish_time" value="{{ .form.PublishTime }}">
+                <input type="date" class="form-control form-control-sm {{ if $dateErr }}is-invalid{{ end }}" name="publish_time" value="{{ .form.PublishTime }}">
                 {{ if $dateErr }}
                 <div class="invalid-feedback">
                     {{ $dateErr }}
@@ -81,10 +86,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Краткий текст</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Краткий текст</label>
             <div class="col-sm-10">
                 {{ $ptErr := index .errors "ArticleForm.PreviewText" }}
-                <textarea id="editor-preview" class="form-control {{ if $ptErr }}is-invalid{{ end }}" name="preview_text">{{ .form.PreviewText }}</textarea>
+                <textarea id="editor-preview" class="form-control form-control-sm {{ if $ptErr }}is-invalid{{ end }}" name="preview_text">{{ .form.PreviewText }}</textarea>
                 {{ if $ptErr }}
                 <div class="invalid-feedback">
                     {{ $ptErr }}
@@ -93,10 +98,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Текст</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Текст</label>
             <div class="col-sm-10">
                 {{ $tErr := index .errors "ArticleForm.Text" }}
-                <textarea id="editor-text" class="form-control {{ if $tErr }}is-invalid{{ end }}" name="text">{{ .form.Text }}</textarea>
+                <textarea id="editor-text" class="form-control form-control-sm {{ if $tErr }}is-invalid{{ end }}" name="text">{{ .form.Text }}</textarea>
                 {{ if $tErr }}
                 <div class="invalid-feedback">
                     {{ $tErr }}
@@ -105,10 +110,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Meta keywords</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Meta keywords</label>
             <div class="col-sm-10">
                 {{ $mkErr := index .errors "ArticleForm.MetaKeywords" }}
-                <input type="text" class="form-control {{ if $mkErr }}is-invalid{{ end }}" name="meta_keywords" value="{{ .form.MetaKeywords }}">
+                <input type="text" class="form-control form-control-sm {{ if $mkErr }}is-invalid{{ end }}" name="meta_keywords" value="{{ .form.MetaKeywords }}">
                 {{ if $mkErr }}
                 <div class="invalid-feedback">
                     {{ $mkErr }}
@@ -117,10 +122,10 @@
             </div>
         </div>
         <div class="row mb-3 mt-3">
-            <label class="col-sm-2 col-form-label">Meta description</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Meta description</label>
             <div class="col-sm-10">
                 {{ $mdErr := index .errors "ArticleForm.MetaDescription" }}
-                <input type="text" class="form-control {{ if $mdErr }}is-invalid{{ end }}" name="meta_description" value="{{ .form.MetaDescription }}">
+                <input type="text" class="form-control form-control-sm {{ if $mdErr }}is-invalid{{ end }}" name="meta_description" value="{{ .form.MetaDescription }}">
                 {{ if $mdErr }}
                 <div class="invalid-feedback">
                     {{ $mdErr }}
@@ -129,7 +134,7 @@
             </div>
         </div>
         <div class="row mb-3 mt-3 border-bottom">
-            <label class="col-sm-2 col-form-label">Теги</label>
+            <label class="col-sm-2 col-form-label col-form-label-sm">Теги</label>
             <div class="col-sm-10">
                 {{ $tags := .form.Tags }}
                 {{ range .tags }}
@@ -142,7 +147,7 @@
                 {{ end }}
             </div>
         </div>
-        <a href="/admin" class="btn btn-secondary">Назад</a>
-        <button type="submit" class="btn btn-primary">Сохранить</button>
+        <a href="/admin" class="btn btn-sm btn-secondary">Назад</a>
+        <button type="submit" class="btn btn-sm btn-primary">Сохранить</button>
     </form>
 </main>