forked from PrzemekFortuna/iTram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TripService.cs
100 lines (80 loc) · 3.22 KB
/
TripService.cs
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
95
96
97
98
99
100
using AutoMapper;
using DBConnection;
using DBConnection.DTO;
using DBConnection.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Services
{
public class TripService
{
private static decimal costPerMinute = 0.1m;
private TramContext _context;
private IMapper _mapper;
private readonly IHttpContextAccessor _httpContextAccessor;
public TripService(TramContext context, IMapper mapper, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_mapper = mapper;
_httpContextAccessor = httpContextAccessor;
}
public async Task<TripResDTO> CreateTrip(TripReqDTO tripDTO)
{
var userId = Int32.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
var tripExists = await ActiveTripForUserExists(userId);
if (tripExists)
throw new InvalidOperationException("There is already an active trip for user");
var trip = new Trip()
{
IsFinished = false,
StartTime = DateTime.Now,
TramId = tripDTO.TramId,
UserId = userId,
Length = 0f
};
var tr = await _context.Trips.AddAsync(trip);
await _context.SaveChangesAsync();
return _mapper.Map<TripResDTO>(tr.Entity);
}
public async Task<ICollection<TripResDTO>> GetTripsForUser()
{
var userId = Int32.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
var trips = await _context.Trips.Where(t => t.UserId == userId)
.Select(tr => _mapper.Map<TripResDTO>(tr)).ToListAsync();
return trips;
}
public async Task<TripResDTO> GetTrip(int id)
{
var tr = await _context.Trips.FindAsync(id);
return _mapper.Map<TripResDTO>(tr);
}
public async Task<decimal> FinishTrip()
{
var userId = Int32.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
var tripExists = await ActiveTripForUserExists(userId);
if (!tripExists)
throw new InvalidOperationException("There is no active trip for this user");
var trip = await _context.Trips.SingleAsync(t => t.UserId == userId && t.IsFinished == false);
trip.IsFinished = true;
trip.FinishTime = DateTime.Now;
var cost = (trip.FinishTime - trip.StartTime).Minutes * costPerMinute;
await _context.SaveChangesAsync();
return cost;
}
private async Task<bool> ActiveTripForUserExists(int userId)
{
var tr = await _context.Trips.AnyAsync(t => t.UserId == userId && t.IsFinished == false);
return tr;
}
private void ThrowActiveTripExists()
{
throw new InvalidOperationException("There is already an active trip for user");
}
}
}