Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

feat(templates): implement search across name and description fields

Jonas Kaninda6fd8507f18f5
diff --git a/internal/handlers/template_handler.go b/internal/handlers/template_handler.go
index d23fe16..79da37e 100644
--- a/internal/handlers/template_handler.go
+++ b/internal/handlers/template_handler.go
@@ -58,6 +58,11 @@ type UpdateTemplateRequest struct {
type DeleteTemplateRequest struct {
ID int `param:"id"`
}
+type ListTemplatesRequest struct {
+ Page int `query:"page" default:"0"`
+ Size int `query:"size" default:"20"`
+ Search string `query:"search"`
+}
type PreviewTemplateRequest struct {
Body struct {
SubjectTemplate string `json:"subject_template" required:"true"`
@@ -160,10 +165,10 @@ func (h *TemplateHandler) Update(c *okapi.Context, req *UpdateTemplateRequest) e
return ok(c, tmpl)
}
-func (h *TemplateHandler) List(c *okapi.Context, req *ListRequest) error {
+func (h *TemplateHandler) List(c *okapi.Context, req *ListTemplatesRequest) error {
page, size, offset := normalizePageParams(req.Page, req.Size)
- templates, total, err := h.repo.FindByScope(getScope(c), size, offset)
+ templates, total, err := h.repo.FindByScope(getScope(c), req.Search, size, offset)
if err != nil {
return c.AbortInternalServerError("failed to list templates")
}
diff --git a/internal/handlers/user_data_handler.go b/internal/handlers/user_data_handler.go
index 7756819..f338189 100644
--- a/internal/handlers/user_data_handler.go
+++ b/internal/handlers/user_data_handler.go
@@ -104,7 +104,7 @@ func (h *UserDataHandler) Export(c *okapi.Context) error {
userID := uint(c.GetInt("user_id"))
scope := repositories.ResourceScope{UserID: userID}
- templates, _, err := h.templateRepo.FindByScope(scope, exportLimit, 0)
+ templates, _, err := h.templateRepo.FindByScope(scope, "", exportLimit, 0)
if err != nil {
return c.AbortInternalServerError("failed to load templates")
}
diff --git a/internal/handlers/workspace_data_handler.go b/internal/handlers/workspace_data_handler.go
index 0903dad..493e4b9 100644
--- a/internal/handlers/workspace_data_handler.go
+++ b/internal/handlers/workspace_data_handler.go
@@ -110,7 +110,7 @@ func (h *WorkspaceDataHandler) Export(c *okapi.Context) error {
return c.AbortNotFound("workspace not found")
}
- templates, _, err := h.templateRepo.FindByScope(scope, exportLimit, 0)
+ templates, _, err := h.templateRepo.FindByScope(scope, "", exportLimit, 0)
if err != nil {
return c.AbortInternalServerError("failed to load templates")
}
diff --git a/internal/routes/user_routes.go b/internal/routes/user_routes.go
index 8098ca9..28c23d8 100644
--- a/internal/routes/user_routes.go
+++ b/internal/routes/user_routes.go
@@ -320,7 +320,7 @@ func (r *Router) userRoutes() []okapi.RouteDefinition {
Handler: okapi.H(r.h.template.List),
Group: userGroup,
Summary: "List templates",
- Request: &handlers.ListRequest{},
+ Request: &handlers.ListTemplatesRequest{},
Response: &dto.PageableResponse[models.Template]{},
},
{
diff --git a/internal/storage/repositories/template_repo.go b/internal/storage/repositories/template_repo.go
index 45ef99e..b0c4351 100644
--- a/internal/storage/repositories/template_repo.go
+++ b/internal/storage/repositories/template_repo.go
@@ -88,13 +88,21 @@ func (r *TemplateRepository) FindByWorkspaceID(workspaceID uint, limit, offset i
return templates, total, nil
}
-func (r *TemplateRepository) FindByScope(scope ResourceScope, limit, offset int) ([]models.Template, int64, error) {
+func (r *TemplateRepository) FindByScope(scope ResourceScope, search string, limit, offset int) ([]models.Template, int64, error) {
var items []models.Template
var total int64
- ApplyScope(r.db.Model(&models.Template{}), scope).Count(&total)
+ q := ApplyScope(r.db.Model(&models.Template{}), scope)
+ if search != "" {
+ q = q.Where("name ILIKE ? OR description ILIKE ?", "%"+search+"%", "%"+search+"%")
+ }
+ q.Count(&total)
- if err := ApplyScope(r.db, scope).
+ qItems := ApplyScope(r.db, scope)
+ if search != "" {
+ qItems = qItems.Where("name ILIKE ? OR description ILIKE ?", "%"+search+"%", "%"+search+"%")
+ }
+ if err := qItems.
Order("created_at DESC").
Limit(limit).Offset(offset).
Find(&items).Error; err != nil {
diff --git a/web/src/api/templates.ts b/web/src/api/templates.ts
index 58319f5..837c2bc 100644
--- a/web/src/api/templates.ts
+++ b/web/src/api/templates.ts
@@ -15,8 +15,8 @@ import type {
} from './types'
export const templatesApi = {
- list(page = 0, size = 20) {
- return api.get<PaginatedResponse<Template>>('/users/me/templates', { params: { page, size } })
+ list(page = 0, size = 20, search = '') {
+ return api.get<PaginatedResponse<Template>>('/users/me/templates', { params: { page, size, search } })
},
create(data: TemplateInput) {
return api.post<ApiResponse<Template>>('/users/me/templates', data)
diff --git a/web/src/views/templates/Templates.vue b/web/src/views/templates/Templates.vue
index fcc3e51..46e7877 100644
--- a/web/src/views/templates/Templates.vue
+++ b/web/src/views/templates/Templates.vue
@@ -29,6 +29,8 @@ const pageable = ref<Pageable>({
empty: true,
});
const loading = ref(true);
+const search = ref('');
+let searchTimeout: ReturnType<typeof setTimeout> | null = null;
const showModal = ref(false);
const editing = ref<Template | null>(null);
@@ -81,7 +83,7 @@ function closeModal() {
async function loadTemplates(page = 0) {
loading.value = true;
try {
- const res = await templatesApi.list(page, pageable.value.size);
+ const res = await templatesApi.list(page, pageable.value.size, search.value);
templates.value = res.data.data;
pageable.value = res.data.pageable;
} catch {
@@ -91,6 +93,11 @@ async function loadTemplates(page = 0) {
}
}
+function onSearchInput() {
+ if (searchTimeout) clearTimeout(searchTimeout);
+ searchTimeout = setTimeout(() => loadTemplates(0), 300);
+}
+
async function saveTemplate() {
if (!form.value.name.trim()) return;
saving.value = true;
@@ -217,14 +224,25 @@ onMounted(() => {
</div>
</div>
+ <div class="card">
+ <div class="flex gap-2 mb-4">
+ <input
+ v-model="search"
+ class="form-input"
+ placeholder="Search by name or description..."
+ style="max-width: 320px"
+ @input="onSearchInput"
+ />
+ </div>
+
<div v-if="loading" class="loading-page">
<div class="spinner"></div>
</div>
- <div v-else class="card">
+ <div v-else>
<div v-if="templates.length === 0" class="empty-state">
- <h3>No Templates</h3>
- <p>Create your first template to reuse email layouts.</p>
+ <h3>{{ search ? 'No Results' : 'No Templates' }}</h3>
+ <p>{{ search ? 'No templates match your search.' : 'Create your first template to reuse email layouts.' }}</p>
</div>
<template v-else>
@@ -329,6 +347,7 @@ onMounted(() => {
</div>
</template>
</div>
+ </div>
<!-- Create/Edit Template Modal -->
<div