File size: 1,391 Bytes
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { success } from '../../lib/response.js'
import { prisma } from '../../config/database.js'
import * as landingService from './landing.service.js'

export async function getHome(req, res, next) {
  try {
    const [featuredTemplates, templatesCount, clientsCount, projectsCount] = await Promise.all([
      prisma.template.findMany({
        where: { isPublished: true },
        select: { id: true, title: true, description: true, category: true, price: true, previewUrl: true },
        orderBy: { sortOrder: 'asc' },
        take: 8
      }),
      prisma.template.count({ where: { isPublished: true } }),
      prisma.user.count({ where: { role: 'CLIENT' } }),
      prisma.order.count(),
    ])
    success(res, {
      featuredTemplates,
      stats: { templates: templatesCount, clients: clientsCount, projects: projectsCount }
    })
  } catch (err) { next(err) }
}

export async function getContent(req, res, next) {
  try {
    const data = await landingService.getAllSections()
    success(res, data)
  } catch (err) { next(err) }
}

export async function getCategories(req, res, next) {
  try {
    const categories = await prisma.template.groupBy({
      by: ['category'],
      _count: true,
      where: { isPublished: true }
    })
    success(res, {
      categories: categories.map(c => ({ name: c.category, count: c._count }))
    })
  } catch (err) { next(err) }
}