-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitService.cs
More file actions
70 lines (58 loc) · 2 KB
/
VisitService.cs
File metadata and controls
70 lines (58 loc) · 2 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
using Microsoft.EntityFrameworkCore;
using URLShortener.Domain.Entities;
using URLShortener.Shared.Data;
using URLShortener.Shared.Models.Visit;
using URLShortener.Shared.Services.Interfaces;
namespace URLShortener.Services.Implementations;
public class VisitService : IVisitService
{
private readonly IApplicationDbContext _context;
public VisitService(IApplicationDbContext context)
{
_context = context;
}
public async Task<List<Visit>> GelAllByLinkIdAsync(GelAllByLinkIdModel model)
{
var result = await _context.Links
.Where(l => l.Id == model.Id && l.UserId == model.UserId)
.Include(l => l.Visits)
.SelectMany(s => s.Visits)
.AsNoTracking()
.ToListAsync();
return result;
}
public async Task<ICollection<Visit>> GetAllByUserIdAsync(GetAllByUserIdModel model)
{
var visits = await _context.Links.Where(l => l.UserId == model.UserId)
.Include(l => l.Visits)
.SelectMany(s => s.Visits)
.AsNoTracking()
.ToListAsync();
return visits;
}
public async Task<Visit> CreateAsync(CreateVisitModel model)
{
var visit = new Visit
{
LinkId = model.LinkId,
IpAddress = model.IpAddress,
Country = model.Country,
City = model.City
};
await _context.Visits.AddAsync(visit);
await _context.SaveChangesAsync();
return visit;
}
public async Task UpdateGeoDataAsync(UpdateGeoDataModel model)
{
var visit = await _context.Visits.SingleAsync(v => v.Id == model.VisitId);
visit.Country = model.Country;
visit.City = model.City;
await _context.SaveChangesAsync();
}
public async Task<bool> IsDeletedAsync(IsDeletedModel model)
{
var visit= await _context.Visits.AsNoTracking().Where(v => v.LinkId == model.LinkId).SingleAsync(v => v.Id == model.Id);
return visit.IsDeleted;
}
}