-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherRepository.cs
More file actions
135 lines (115 loc) · 5.16 KB
/
WeatherRepository.cs
File metadata and controls
135 lines (115 loc) · 5.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Threading;
using System.Threading.Tasks;
using HappyCode.NetCoreBoilerplate.Core.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using HappyCode.NetCoreBoilerplate.Core.Dtos;
using HappyCode.NetCoreBoilerplate.Core.Extensions;
using Microsoft.Extensions.Logging;
namespace HappyCode.NetCoreBoilerplate.Core.Repositories
{
public interface IWeatherRepository
{
Task<List<WeatherDto>> GetAllAsync(CancellationToken cancellationToken);
Task<WeatherDto> GetByIdAsync(int id, CancellationToken cancellationToken);
Task<WeatherDetailsDto> GetByIdWithDetailsAsync(object id, CancellationToken cancellationToken);
Task<WeatherDto> GetOldestAsync(CancellationToken cancellationToken);
Task<bool> DeleteByIdAsync(int id, CancellationToken cancellationToken);
Task<Weather> InsertAsync(WeatherPostDto employeePostDto, CancellationToken cancellationToken);
Task<WeatherDto> UpdateAsync(int id, WeatherPutDto employeePutDto, CancellationToken cancellationToken);
Task<WeatherCollection> InsertMultiAsync(Weather[] weatherList, CancellationToken cancellationToken);
}
internal class WeatherRepository : RepositoryBase<Weather>, IWeatherRepository
{
public WeatherRepository(WeatherContext dbContext, ILogger<WeatherRepository> logger) : base(dbContext)
{ }
public async Task<List<WeatherDto>> GetAllAsync (CancellationToken cancellationToken) {
var weatherList = await DbContext.Weather
.AsNoTracking()
.ToListAsync(cancellationToken);
return weatherList.Select(WeatherExtensions.MapToDto).ToList();
}
public async Task<WeatherDto> GetByIdAsync(int id, CancellationToken cancellationToken)
{
var emp = await DbContext.Weather
.AsNoTracking()
.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
if (emp == null)
{
return null;
}
return emp.MapToDto();
}
public async Task<WeatherDetailsDto> GetByIdWithDetailsAsync(object id, CancellationToken cancellationToken)
{
var emp = await DbContext.Weather
.Include(x => x.Id)
.SingleOrDefaultAsync(x => x.Id == (int)id, cancellationToken);
if (emp == null) { return null; }
return new WeatherDetailsDto
{
Id = emp.Id,
Time = emp.Time,
Humidity = emp.Humidity,
Temperature = emp.Temperature,
WindSpeed = emp.WindSpeed,
};
}
public async Task<WeatherDto> GetOldestAsync(CancellationToken cancellationToken)
{
var emp = await DbContext.Weather
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(cancellationToken);
if (emp == null) { return null; }
return emp.MapToDto();
}
public async Task<Weather> InsertAsync(WeatherPostDto weatherPostDto, CancellationToken cancellationToken)
{
var weather = new Weather
{
Time = weatherPostDto.Time,
Humidity = weatherPostDto.Humidity,
Temperature = weatherPostDto.Temperature,
WindSpeed = weatherPostDto.WindSpeed,
};
await DbContext.Weather.AddAsync(weather, cancellationToken);
await DbContext.SaveChangesAsync(cancellationToken);
return weather;
}
public async Task<WeatherCollection> InsertMultiAsync(Weather[] weatherList, CancellationToken cancellationToken)
{
try
{
WeatherCollection collection = new WeatherCollection();
collection.AddRange(weatherList);
await DbContext.Weather.AddRangeAsync(collection);
await DbContext.SaveChangesAsync(cancellationToken);
return collection;
}
catch (Exception Ex)
{
Console.WriteLine("[InsertMultiAsync] Throw Exception {Ex}");
throw;
}
}
public async Task<WeatherDto> UpdateAsync(int id, WeatherPutDto weatherPutDto, CancellationToken cancellationToken)
{
var emp = await DbContext.Weather
.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
if (emp is null)
{ return null; }
emp.Id = weatherPutDto.Humidity;
await DbContext.SaveChangesAsync(cancellationToken);
return emp.MapToDto();
}
public async Task<bool> DeleteByIdAsync(int id, CancellationToken cancellationToken)
{
var emp = await DbContext.Weather
.SingleOrDefaultAsync(x => x.Id == id, cancellationToken);
if (emp == null)
{ return false; }
DbContext.Weather.Remove(emp);
return await DbContext.SaveChangesAsync(cancellationToken) > 0;
}
}
}