Spaces:
Running

File size: 1,949 Bytes
7039b6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d07d79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7039b6a
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using FlowAPI.Application.Interfaces;
using FlowAPI.Domain.Entities;
using FlowAPI.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FlowAPI.Infrastructure.Repositories
{
    public class CustomTemplateRepository : ICustomTemplateRepository
    {
        private readonly AppDbContext _context;

        public CustomTemplateRepository(AppDbContext context)
        {
            _context = context;
        }

        public async Task<CustomTemplate?> GetByIdAsync(Guid id)
        {
            return await _context.CustomTemplates.FirstOrDefaultAsync(t => t.Id == id);
        }

        public async Task<IEnumerable<CustomTemplate>> GetByUserIdAsync(Guid userId)
        {
            return await _context.CustomTemplates
                .Where(t => t.UserId == userId)
                .OrderByDescending(t => t.CreatedAt)
                .ToListAsync();
        }

        public async Task<CustomTemplate> CreateAsync(CustomTemplate template)
        {
            _context.CustomTemplates.Add(template);
            await _context.SaveChangesAsync();
            return template;
        }

        public async Task<CustomTemplate> UpdateAsync(CustomTemplate template)
        {
            _context.CustomTemplates.Update(template);
            await _context.SaveChangesAsync();
            return template;
        }

        public async Task<bool> DeleteAsync(Guid id)
        {
            var template = await _context.CustomTemplates.FindAsync(id);
            if (template is null) return false;
            _context.CustomTemplates.Remove(template);
            await _context.SaveChangesAsync();
            return true;
        }

        public async Task<int> CountByUserIdAsync(Guid userId)
        {
            return await _context.CustomTemplates.CountAsync(t => t.UserId == userId);
        }
    }
}