source: Application/Dal/ApplicationStorage/DataAccess/Concrete/SearchDa.cs

Last change on this file was f5f7c24, checked in by 192011 <mk.snicker@…>, 17 months ago

Initial commit

  • Property mode set to 100644
File size: 10.3 KB
Line 
1using Dal.ApplicationStorage.DataAccess.Abstract;
2using Microsoft.CodeAnalysis;
3using Microsoft.EntityFrameworkCore;
4using Microsoft.Extensions.Logging;
5using Microsoft.Win32;
6using Models.DatabaseModels;
7using Models.DataTransferObjects.Serach;
8using Models.JSON;
9using System;
10using System.Collections.Generic;
11using System.Linq;
12using System.Text;
13using System.Threading.Tasks;
14
15namespace Dal.ApplicationStorage.DataAccess.Concrete
16{
17 public class SearchDa : ISearchDa
18 {
19 private readonly ApiContext _db;
20 private static ILogger<SearchDa> _logger;
21
22 public SearchDa(ApiContext db, ILogger<SearchDa> logger)
23 {
24 _db = db;
25 _logger = logger;
26 }
27 public async Task<List<SearchLocationDTO>> GetLocations()
28 {
29 try
30 {
31 var citiesFromDb = await _db.Locations.ToListAsync();
32 List<SearchLocationDTO> cities = new List<SearchLocationDTO>();
33 foreach(var city in citiesFromDb)
34 {
35 cities.Add(new SearchLocationDTO()
36 {
37 CityName = city.City,
38 });
39 }
40
41 return cities.DistinctBy(x => x.CityName).ToList();
42 }
43 catch (Exception e)
44 {
45 _logger.LogError(e.Message);
46 throw;
47 }
48 }
49 public async Task<List<SearchBrandsDTO>> GetVehicleBrands()
50 {
51 try
52 {
53 var brandsFromDb = await _db.Vehicles.ToListAsync();
54 List<SearchBrandsDTO> brands = new List<SearchBrandsDTO>();
55 foreach(var brand in brandsFromDb)
56 {
57 brands.Add(new SearchBrandsDTO()
58 {
59 BrandName = brand.Brand
60 });
61 }
62
63 return brands.DistinctBy(x => x.BrandName).ToList();
64 }
65 catch (Exception e)
66 {
67 _logger.LogError(e.Message);
68 throw;
69 }
70 }
71 public async Task<List<CarsDTO>> GetVehiclesBySearchParameters(SearchJSON data)
72 {
73 try
74 {
75 var query = await (from vehicles in _db.Vehicles
76 join locations in _db.Locations on vehicles.LocationId equals locations.LocationId
77 join models in _db.Models on vehicles.ModelId equals models.ModelId
78 join registrations in _db.Registrations on vehicles.RegistrationId equals registrations.RegistrationId
79 join companies in _db.Companies on vehicles.CompanyId equals companies.CompanyId
80 select new CarsDTO()
81 {
82 CarId = vehicles.VehicleId,
83 Brand = vehicles.Brand,
84 Model = models.ModelName,
85 Color = models.Color,
86 ModelYear = models.ModelYear.Year,
87 NumSeats = models.NumOfSeats,
88 NumOfDoors = models.NumOfDoors,
89 Fuel = models.Fuel,
90 Transmission = models.Transmission,
91 FuelEfficiency = vehicles.FuelEfficiency,
92 DailyRentalPrice = (double)vehicles.DailyRentalPrice,
93 City = locations.City,
94 IsAvailable = registrations.IsAvailable,
95 ImgUrl = models.ImgUrl,
96 CompanyName = companies.CompanyName
97 }
98 ).Where(x => x.Brand == data.Brand && x.City == data.City && x.IsAvailable)
99 .ToListAsync();
100
101 return query;
102 }
103 catch (Exception e)
104 {
105 _logger.LogError(e.Message);
106 throw;
107 }
108 }
109 public async Task<List<CarsDTO>> GetAllVehicles()
110 {
111 try
112 {
113 var query = await (from vehicles in _db.Vehicles
114 join locations in _db.Locations on vehicles.LocationId equals locations.LocationId
115 join models in _db.Models on vehicles.ModelId equals models.ModelId
116 join registrations in _db.Registrations on vehicles.RegistrationId equals registrations.RegistrationId
117 join companies in _db.Companies on vehicles.CompanyId equals companies.CompanyId
118 select new CarsDTO()
119 {
120 CarId = vehicles.VehicleId,
121 Brand = vehicles.Brand,
122 Model = models.ModelName,
123 Color = models.Color,
124 ModelYear = models.ModelYear.Year,
125 NumSeats = models.NumOfSeats,
126 NumOfDoors = models.NumOfDoors,
127 Fuel = models.Fuel,
128 Transmission = models.Transmission,
129 FuelEfficiency = vehicles.FuelEfficiency,
130 DailyRentalPrice = (double)vehicles.DailyRentalPrice,
131 City = locations.City,
132 IsAvailable = registrations.IsAvailable,
133 ImgUrl = models.ImgUrl,
134 CompanyName = companies.CompanyName
135 }
136 ).Where(x => x.IsAvailable)
137 .ToListAsync();
138
139 return query;
140 }
141 catch (Exception e)
142 {
143 _logger.LogError(e.Message);
144 throw;
145 }
146 }
147 public async Task<List<CarsDTO>> GetVehilcesByLocation(SearchJSON data)
148 {
149 try
150 {
151 var query = await (from vehicles in _db.Vehicles
152 join locations in _db.Locations on vehicles.LocationId equals locations.LocationId
153 join models in _db.Models on vehicles.ModelId equals models.ModelId
154 join registrations in _db.Registrations on vehicles.RegistrationId equals registrations.RegistrationId
155 join companies in _db.Companies on vehicles.CompanyId equals companies.CompanyId
156 select new CarsDTO()
157 {
158 CarId = vehicles.VehicleId,
159 Brand = vehicles.Brand,
160 Model = models.ModelName,
161 Color = models.Color,
162 ModelYear = models.ModelYear.Year,
163 NumSeats = models.NumOfSeats,
164 NumOfDoors = models.NumOfDoors,
165 Fuel = models.Fuel,
166 Transmission = models.Transmission,
167 FuelEfficiency = vehicles.FuelEfficiency,
168 DailyRentalPrice = (double)vehicles.DailyRentalPrice,
169 City = locations.City,
170 IsAvailable = registrations.IsAvailable,
171 ImgUrl = models.ImgUrl,
172 CompanyName = companies.CompanyName
173 }
174 ).Where(x => x.City == data.City && x.IsAvailable)
175 .ToListAsync();
176
177 return query;
178 }
179 catch (Exception e)
180 {
181 _logger.LogError(e.Message);
182 throw;
183 }
184 }
185 public async Task<List<CarsDTO>> GetVehiclesByBrand(SearchJSON data)
186 {
187 try
188 {
189 var query = await (from vehicles in _db.Vehicles
190 join locations in _db.Locations on vehicles.LocationId equals locations.LocationId
191 join models in _db.Models on vehicles.ModelId equals models.ModelId
192 join registrations in _db.Registrations on vehicles.RegistrationId equals registrations.RegistrationId
193 join companies in _db.Companies on vehicles.CompanyId equals companies.CompanyId
194 select new CarsDTO()
195 {
196 CarId = vehicles.VehicleId,
197 Brand = vehicles.Brand,
198 Model = models.ModelName,
199 Color = models.Color,
200 ModelYear = models.ModelYear.Year,
201 NumSeats = models.NumOfSeats,
202 NumOfDoors = models.NumOfDoors,
203 Fuel = models.Fuel,
204 Transmission = models.Transmission,
205 FuelEfficiency = vehicles.FuelEfficiency,
206 DailyRentalPrice = (double)vehicles.DailyRentalPrice,
207 City = locations.City,
208 IsAvailable = registrations.IsAvailable,
209 ImgUrl = models.ImgUrl,
210 CompanyName = companies.CompanyName
211 }
212 ).Where(x => x.Brand == data.Brand.Trim() && x.IsAvailable)
213 .ToListAsync();
214
215 return query;
216 }
217 catch (Exception e)
218 {
219 _logger.LogError(e.Message);
220 throw;
221 }
222 }
223 }
224}
Note: See TracBrowser for help on using the repository browser.