-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkService.cs
More file actions
94 lines (82 loc) · 3.01 KB
/
LinkService.cs
File metadata and controls
94 lines (82 loc) · 3.01 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Microsoft.EntityFrameworkCore;
using URLShortener.Domain.Entities;
using URLShortener.Shared.Data;
using URLShortener.Shared.Models.Link;
using URLShortener.Shared.Services.Interfaces;
namespace URLShortener.Services.Implementations;
public class LinkService : ILinkService
{
private readonly IApplicationDbContext _context;
public LinkService(IApplicationDbContext context)
{
_context = context;
}
public async Task<Link> CreateAsync(CreateLinkModel model)
{
var link = new Link
{
ShortAddress = model.ShortAddress,
FullAddress = model.FullAddress,
UserId = model.UserId
};
await _context.Links.AddAsync(link);
await _context.SaveChangesAsync();
return link;
}
public async Task<Link> UpdateShortAddressAsync(UpdateShortAddressModel model)
{
var link = await GetByIdAsync(new GetByIdModel { UserId = model.UserId, Id = model.Id });
link.ShortAddress = model.ShortAddress;
await _context.SaveChangesAsync();
return link;
}
public async Task<Link> UpdateFullAddressAsync(UpdateFullAddressModel model)
{
var link = await GetByIdAsync(new GetByIdModel { UserId = model.UserId, Id = model.Id });
link.FullAddress = model.FullAddress;
await _context.SaveChangesAsync();
return link;
}
public async Task<Link> SetEnabledAsync(SetEnabledModel model)
{
var link = await GetByIdAsync(new GetByIdModel { UserId = model.UserId, Id = model.Id });
link.IsEnabled = model.IsEnabled;
await _context.SaveChangesAsync();
return link;
}
public async Task<ICollection<Link>> GetAllByUserIdAsync(Guid userId)
{
var linkList = await _context.Links
.Where(l => l.UserId == userId)
.AsNoTracking()
.ToListAsync();
return linkList;
}
public async Task<Link> GetByIdAsync(GetByIdModel model, bool includeVisits = false, bool asNoTracking = false)
{
var query = _context.Links.AsQueryable();
if (includeVisits) query = query.Include(l => l.Visits);
if (asNoTracking) query.AsNoTracking();
return await query
.Where(l => l.UserId == model.UserId)
.SingleAsync(l => l.Id == model.Id);
}
public async Task<Link> GetByShortAddressAsync(GetByShortAddressModel model)
{
var link = await _context.Links
.AsNoTracking()
.SingleAsync(l => l.ShortAddress == model.ShortAddress);
return link;
}
public async Task DeleteAsync(DeleteModel model)
{
var link = await GetByIdAsync(new GetByIdModel { UserId = model.UserId, Id = model.Id });
link.IsDeleted = true;
await _context.SaveChangesAsync();
}
public async Task<bool> IsDeletedAsync(IsDeletedModel model)
{
var link= await _context.Links.AsNoTracking().Where(l => l.UserId == model.UserId).SingleAsync(l => l.Id == model.Id);
return link.IsDeleted;
}
}