Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.61 KB | None | 0 0
  1. using AutoMapper;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Timerman.Repository;
  9. using Timerman.Service.DTO.Entities;
  10. using Timerman.Service.Interfaces;
  11. using Microsoft.EntityFrameworkCore;
  12. using Timerman.Service.Infrastructure;
  13. using Timerman.Data.Entities;
  14. using Timerman.Service.Infrastructure.Extentions;
  15. using Microsoft.AspNetCore.Http;
  16. using AutoMapper.QueryableExtensions;
  17. using System.Net.Http;
  18. using System.Net.Http.Headers;
  19. using Newtonsoft.Json;
  20.  
  21.  
  22. namespace Timerman.Service.Services
  23. {
  24. public class ParticipantRR
  25. {
  26. public string EventCode { get; set; }
  27. public string Distance { get; set; }
  28. public string FirstName {get; set; }
  29. public string LastName { get; set; }
  30. public int StartNumber { get; set; }
  31. public int? AbsoluteTimeResult { get; set; }
  32. public int? IndividualTimeResult { get; set; }
  33. public string Category {get; set;}
  34. public int Position { get; set; }
  35. public int GenderAgeNominationPosition { get; set; }
  36. public int GenderNominationPosition { get; set; }
  37. public int AgeNominationPosition { get; set; }
  38.  
  39.  
  40. }
  41. public class EventService : IEventService
  42. {
  43. private readonly IUnitOfWork _uow;
  44. private readonly IMapper _mapper;
  45. private readonly ICoreService _coreService;
  46. private readonly ILogger _logger;
  47.  
  48. private readonly IFileWorkService _fileWorkService;
  49.  
  50. public EventService(
  51. IUnitOfWork uow,
  52. IMapper mapper,
  53. ICoreService filter,
  54. IFileWorkService fileWorkService,
  55. ILoggerFactory loggerFactory)
  56. {
  57. _uow = uow;
  58. _mapper = mapper;
  59. _coreService = filter;
  60. _logger = loggerFactory.CreateLogger<EventService>();
  61. _fileWorkService = fileWorkService;
  62. }
  63. #region EventEntity
  64. public async Task<EventDTO> AddEventAsync(EventDTO item, string userId, IFormFile file, IFormFile filePreview)
  65. {
  66. try
  67. {
  68. var eventItem = _mapper.Map<Event>(item);
  69.  
  70. eventItem.AddedBy = userId;
  71. eventItem.AddedDate = DateTime.UtcNow;
  72.  
  73. var srcPath = "";
  74. var srcPathPreview = "";
  75. if (file != null)
  76. srcPath = _fileWorkService.PhotoSave(file);
  77.  
  78. if (filePreview != null)
  79. srcPathPreview = _fileWorkService.PhotoSave(filePreview);
  80.  
  81. eventItem.Photo = srcPath;
  82. eventItem.PhotoPreview = srcPathPreview;
  83.  
  84. _logger.LogDebug($"Mapped entity: {eventItem.ToString()}");
  85.  
  86. var result = await _uow.Events.CreateRAsync(eventItem);
  87.  
  88. await _uow.SaveChangesAsync();
  89.  
  90. eventItem = await _uow.Events.Query()
  91. .Where(e => e.Id == result.Id)
  92. .IncludeEventRelatedData()
  93. .FirstOrDefaultAsync();
  94.  
  95.  
  96. return _mapper.Map<EventDTO>(eventItem);
  97. }
  98. catch (Exception e)
  99. {
  100. throw new UnexpectedException(e.Message);
  101. }
  102. }
  103.  
  104. public async Task DeleteEventAsync(long eventId)
  105. {
  106. try
  107. {
  108. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  109.  
  110. if (eventItem == null)
  111. throw new NotFoundException("Мероприятие не найдено", "");
  112.  
  113. _uow.Events.RemoveById(eventId);
  114.  
  115. await _uow.SaveChangesAsync();
  116. }
  117. catch (NotFoundException)
  118. {
  119. throw;
  120. }
  121. catch (Exception e)
  122. {
  123. throw new UnexpectedException(e.Message);
  124. }
  125. }
  126.  
  127. public async Task<EventDTO> EditEventAsync(long eventId, EventDTO item, string userId, IFormFile file, IFormFile filePreview)
  128. {
  129. try
  130. {
  131. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  132.  
  133. if (eventItem == null)
  134. throw new NotFoundException("Мероприятие не найдено", "");
  135.  
  136. var srcPath = eventItem.Photo;
  137. var srcPathPreview = eventItem.PhotoPreview;
  138.  
  139.  
  140. if (file != null)
  141. srcPath = _fileWorkService.PhotoSave(file);
  142.  
  143. if (filePreview != null)
  144. srcPathPreview = _fileWorkService.PhotoSave(filePreview);
  145.  
  146. _mapper.Map(item, eventItem);
  147.  
  148.  
  149. eventItem.Photo = srcPath;
  150. eventItem.PhotoPreview = srcPathPreview;
  151.  
  152. eventItem.ModifiedBy = userId;
  153. eventItem.ModifiedDate = DateTime.UtcNow;
  154. _logger.LogDebug($"Mapped entity: {eventItem.ToString()}");
  155.  
  156. _uow.Events.Update(eventItem);
  157.  
  158. await _uow.SaveChangesAsync();
  159.  
  160. return _mapper.Map<EventDTO>(eventItem);
  161. }
  162. catch (NotFoundException)
  163. {
  164. throw;
  165. }
  166. catch (Exception e)
  167. {
  168. throw new UnexpectedException(e.Message);
  169. }
  170.  
  171. }
  172.  
  173. /// <summary>
  174. /// Получение мероприятия по его Id.
  175. /// </summary>
  176. /// <param name="eventId"></param>
  177. /// <returns></returns>
  178. public async Task<EventDTO> EventByIdAsync(long eventId)
  179. {
  180. try
  181. {
  182. // TODO: добавить isActive в предикат
  183. var eventItem = await _uow.Events.Query()
  184. .Where(e => e.Id == eventId)
  185. .IncludeEventRelatedData()
  186. .FirstOrDefaultAsync();
  187.  
  188. if (eventItem == null)
  189. throw new NotFoundException("Мероприятие не найдено", "The event is not found");
  190.  
  191. return _mapper.Map<EventDTO>(eventItem);
  192. }
  193. catch (NotFoundException)
  194. {
  195. throw;
  196. }
  197. catch (Exception e)
  198. {
  199. throw new UnexpectedException(e.Message);
  200. }
  201. }
  202.  
  203. public async Task<IList<EventResultDTO>> GetResultForEventAsync(long eventId)
  204. {
  205. try
  206. {
  207. // Maybe needs optimizations
  208. var items = await _uow.EventDistances.Query()
  209. .Include(c => c.Distance.DistanceType)
  210. .Include(c => c.Distance.ParticipationType)
  211. .Include(c => c.Distance.EventParticipantDistances)
  212. .ThenInclude(c => c.EventParticipantResult)
  213. .Include(c => c.Distance.EventParticipantDistances)
  214. .ThenInclude(c => c.EventParticipants.Participant.City)
  215. .Include(c => c.Distance.EventParticipantDistances)
  216. .ThenInclude(c => c.EventParticipants.Participant.AgeCategory)
  217. .Include(c => c.Distance.EventParticipantDistances)
  218. .ThenInclude(c => c.EventParticipants.Participant.ActivityField)
  219. .Include(c => c.Distance.EventParticipantDistances)
  220. .ThenInclude(c => c.EventParticipants.Participant.SportClub)
  221. .Where(e => e.EventId == eventId)
  222. .GroupBy(c => c.Distance)
  223. .ToListAsync();
  224.  
  225. var result = new List<EventResultDTO>();
  226. foreach (var var in items)
  227. {
  228. result.Add(new EventResultDTO
  229. {
  230. Distance = _mapper.Map<DistanceLightDTO>(var.Key),
  231. Result = _mapper.Map<IList<ParticipantResultDTO>>(var.SelectMany(c => c.Distance.EventParticipantDistances
  232. .Where(f => f.EventParticipantResult != null).Select(s => s.EventParticipantResult)))
  233. });
  234. }
  235.  
  236. return result;
  237.  
  238. /* ProjectTo
  239. var items = await _uow.EventDistances.Query()
  240. .Include(c => c.Distance.DistanceType)
  241. .Include(c => c.Distance.ParticipationType)
  242. .Include(c => c.Distance.EventParticipantDistances)
  243. .ThenInclude(c => c.EventParticipantResult)
  244. .Include(c => c.Distance.EventParticipantDistances)
  245. .ThenInclude(c => c.EventParticipants.Participant.City)
  246. .Include(c => c.Distance.EventParticipantDistances)
  247. .ThenInclude(c => c.EventParticipants.Participant.SportClub)
  248. .Where(e => e.EventId == eventId)
  249. .ProjectTo<EventResultDTO>(_mapper.ConfigurationProvider)
  250. .ToListAsync();
  251.  
  252. var result = items.GroupBy(c => c.Distance).Select(c => new EventResultDTO
  253. {
  254. Distance = c.Key,
  255. Result = c.SelectMany(f => f.Result).ToList()
  256. }).ToList();
  257.  
  258. return result;
  259. */
  260. }
  261. catch (Exception e)
  262. {
  263. throw new UnexpectedException(e.Message);
  264. }
  265. }
  266.  
  267. ///<summary>
  268. /// Занесение результатов мероприятий из API Russia Running в базу данных
  269. ///<summary>
  270. ///<returns></returns>
  271. public async Task<string> getEventResultRRAsync(long eventId)
  272. {
  273. try
  274. {
  275. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  276.  
  277. if (eventItem == null)
  278. throw new NotFoundException("Мероприятие не найдено", "");
  279.  
  280. if (eventItem.EventIdRR == null)
  281. throw new NotFoundException("eventIdRR не найден", "");
  282.  
  283. var eventIdRR = eventItem.EventIdRR;
  284.  
  285. var login = "press@timerman.org";
  286.  
  287. var password = "wKrNwpZRDDR8N3J";
  288.  
  289. var static_path = "https://russiarunning.com/api/beneficiary/get-event-results/en?eventId=";
  290.  
  291. HttpClient client = new HttpClient();
  292.  
  293. var path = static_path + eventIdRR;
  294.  
  295. client.DefaultRequestHeaders.Accept.Clear();
  296.  
  297. byte[] authbytes = Encoding.ASCII.GetBytes(login + ":" + password);
  298.  
  299. string base64 = Convert.ToBase64String(authbytes);
  300.  
  301. client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64);
  302.  
  303. var response = await client.PostAsync(path, null);
  304.  
  305. if ((int) response.StatusCode != 200)
  306. throw new NotFoundException("status code" + (int)response.StatusCode, "");
  307.  
  308. var responseString = await response.Content.ReadAsStringAsync();
  309.  
  310. //var responseJson = new JsonResult(responseString);
  311.  
  312. ParticipantRR [] ParticipantsRR = JsonConvert.DeserializeObject<ParticipantRR []>(responseString);
  313.  
  314. return responseString;
  315.  
  316. }
  317. catch(NotFoundException)
  318. {
  319. throw;
  320. }
  321. catch (Exception e)
  322. {
  323. throw new UnexpectedException(e.Message);
  324. }
  325. }
  326.  
  327. ///<summary>
  328. /// Занесение результатов мероприятий из API Russia Running в базу данных
  329. ///<summary>
  330. ///<returns></returns>
  331. public async Task<bool> EventResultRRAsync(string eventId)
  332. {
  333. try
  334. {
  335. var login = "press@timerman.org";
  336.  
  337. var password = "wKrNwpZRDDR8N3J";
  338.  
  339. var static_path = "https://russiarunning.com/api/beneficiary/get-event-results/en?eventId=";
  340.  
  341. HttpClient client = new HttpClient();
  342.  
  343. var path = static_path + eventId;
  344.  
  345. var requestContent = new FormUrlEncodedContent(new[] {
  346. new KeyValuePair<string, string> ("StartDate", "2019-01-06T11:08:42.723Z"),
  347. new KeyValuePair<string, string> ("EndDate", "2019-08-06T11:08:42.723Z"),
  348. });
  349.  
  350. client.DefaultRequestHeaders.Accept.Clear();
  351.  
  352. byte[] authbytes = Encoding.ASCII.GetBytes(login + ":" + password);
  353.  
  354. string base64 = Convert.ToBase64String(authbytes);
  355.  
  356. client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64);
  357.  
  358. var response = await client.PostAsync(path, null);
  359.  
  360. var responseString = await response.Content.ReadAsStringAsync();
  361.  
  362. Console.WriteLine(responseString);
  363.  
  364. return true;
  365. }
  366. catch(NotFoundException)
  367. {
  368. throw;
  369. }
  370. catch (Exception e)
  371. {
  372. throw new UnexpectedException(e.Message);
  373. }
  374. }
  375.  
  376.  
  377. /// <summary>
  378. /// Получение всех мероприятий.
  379. /// </summary>
  380. /// <returns></returns>
  381. public async Task<IList<EventDTO>> GetAllAsync()
  382. {
  383. try
  384. {
  385. var eventItems = await _uow.Events.Query()
  386. .Where(c => c.IsActive == true)
  387. .IncludeEventRelatedData()
  388. .OrderByDescending(c => c.Date)
  389. .ToListAsync();
  390.  
  391. return _mapper.Map<List<EventDTO>>(eventItems);
  392. }
  393. catch (NotFoundException)
  394. {
  395. throw;
  396. }
  397. catch (Exception e)
  398. {
  399. throw new UnexpectedException(e.Message);
  400. }
  401. }
  402.  
  403. public async Task<IList<EventAdminDTO>> GetAllAdminAsync()
  404. {
  405. try
  406. {
  407. var eventItems = await _uow.Events.Query()
  408. .IncludeEventRelatedData()
  409. .ToListAsync();
  410.  
  411. return _mapper.Map<List<EventAdminDTO>>(eventItems);
  412. }
  413. catch (NotFoundException)
  414. {
  415. throw;
  416. }
  417. catch (Exception e)
  418. {
  419. throw new UnexpectedException(e.Message);
  420. }
  421. }
  422.  
  423. public async Task<EventDTO> GetClosestAsync()
  424. {
  425. try
  426. {
  427. var eventItems = await _uow.Events.Query()
  428. .Where(c => c.IsActive == true && c.Date > DateTime.Now)
  429. .IncludeEventRelatedData()
  430. .OrderByDescending(c => c.Date)
  431. .ToListAsync();
  432.  
  433. if (eventItems.Count == 0)
  434. {
  435. throw new NotFoundException("Ближайшее мероприятие не найдено");
  436. }
  437.  
  438. return _mapper.Map<EventDTO>(eventItems.Last());
  439. }
  440. catch (NotFoundException)
  441. {
  442. throw;
  443. }
  444. catch (Exception e)
  445. {
  446. throw new UnexpectedException(e.Message);
  447. }
  448. }
  449. #endregion
  450.  
  451.  
  452. #region Admin panel event configuration
  453.  
  454. public async Task UpdateDistancesAsync(long eventId, IList<DistanceDTO> distances)
  455. {
  456. try
  457. {
  458. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  459. if (eventItem == null)
  460. throw new NotFoundException("Мероприятие не найдено", "");
  461.  
  462. var oldDistanceIds = _uow.EventDistances.Query()
  463. .Where(e => e.EventId == eventId)
  464. .Select(e => e.DistanceId);
  465.  
  466. _uow.EventDistances.Remove(e => e.EventId == eventId);
  467.  
  468. _uow.Distances.Remove(e => oldDistanceIds.Contains(e.Id));
  469.  
  470. var distanceEntities = _mapper.Map<IList<Distance>>(distances);
  471.  
  472. await _uow.Distances.CreateRangeAsync(distanceEntities);
  473.  
  474. var distanceIds = distanceEntities.Select(e => e.Id);
  475.  
  476. List<EventDistance> eventDistances = new List<EventDistance>();
  477.  
  478. foreach (var id in distanceIds)
  479. {
  480. eventDistances.Add(new EventDistance
  481. {
  482. EventId = eventId,
  483. DistanceId = id
  484. });
  485. }
  486.  
  487. await _uow.EventDistances.CreateRangeAsync(eventDistances);
  488.  
  489. await _uow.SaveChangesAsync();
  490. }
  491. catch (NotFoundException)
  492. {
  493. throw;
  494. }
  495. catch (Exception e)
  496. {
  497. throw new UnexpectedException(e.Message);
  498. }
  499. }
  500. public async Task AddDistanceToEventAsync(long eventId, DistanceDTO distance, IFormFile file)
  501. {
  502. try
  503. {
  504. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  505. if (eventItem == null)
  506. throw new NotFoundException("Мероприятие не найдено", "");
  507.  
  508. string photoPath = "";
  509.  
  510. if (file != null)
  511. photoPath = _fileWorkService.PhotoSave(file);
  512.  
  513. var distanceEntity = _mapper.Map<Distance>(distance);
  514.  
  515. _logger.LogInformation($"Mapped entity: {distanceEntity.ToString()}");
  516.  
  517. distance.DistanceMap = photoPath;
  518.  
  519. await _uow.Distances.CreateAsync(distanceEntity);
  520.  
  521. await _uow.EventDistances.CreateAsync(new EventDistance
  522. {
  523. EventId = eventId,
  524. DistanceId = distanceEntity.Id
  525. });
  526.  
  527. var eventParticipationTypes = await _uow.EventParticipantTypes.GetManyAsync(e => e.EventId == eventId);
  528.  
  529. var eventParticipationTypeIds = eventParticipationTypes.Select(e => e.ParticipationTypeId);
  530.  
  531. if (!eventParticipationTypeIds.Contains(distance.ParticipationTypeId))
  532. {
  533. await _uow.EventParticipantTypes.CreateAsync(new EventParticipantType
  534. {
  535. EventId = eventId,
  536. ParticipationTypeId = distance.ParticipationTypeId,
  537. });
  538. }
  539.  
  540. await _uow.SaveChangesAsync();
  541. }
  542. catch (NotFoundException)
  543. {
  544. throw;
  545. }
  546. catch (Exception e)
  547. {
  548. throw new UnexpectedException(e.Message);
  549. }
  550. }
  551.  
  552. public async Task DeleteEventDistanceAsync(long eventId, long distanceId)
  553. {
  554. try
  555. {
  556. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  557. if (eventItem == null)
  558. throw new NotFoundException("Мероприятие не найдено", "");
  559.  
  560. var distanceItem = await _uow.Distances.GetByIdAsync(distanceId);
  561. if (distanceItem == null)
  562. throw new NotFoundException("Дистанция не найдена", "");
  563.  
  564. var eventDistances = await _uow.EventDistances.GetManyAsync(e =>
  565. e.EventId == eventId &&
  566. e.DistanceId != distanceId,
  567. d => d.Distance);
  568.  
  569. var eventParticipationTypes = eventDistances.Select(e => e.Distance.ParticipationTypeId);
  570.  
  571. if (!eventParticipationTypes.Contains(distanceItem.ParticipationTypeId))
  572. {
  573. var eventParticipantType = await _uow.EventParticipantTypes.GetAsync(e =>
  574. e.ParticipationTypeId == distanceItem.ParticipationTypeId);
  575. if (eventParticipantType != null)
  576. _uow.EventParticipantTypes.Remove(e =>
  577. e.ParticipationTypeId == distanceItem.ParticipationTypeId);
  578. }
  579.  
  580. _uow.EventDistances.Remove(e => e.DistanceId == distanceId);
  581.  
  582. _uow.Distances.RemoveById(distanceId);
  583.  
  584. await _uow.SaveChangesAsync();
  585. }
  586. catch (NotFoundException)
  587. {
  588. throw;
  589. }
  590. catch (Exception e)
  591. {
  592. throw new UnexpectedException(e.Message);
  593. }
  594. }
  595.  
  596. public async Task<DistanceViewDTO> GetEventDistanceByIdAsync(long eventId, long distanceId)
  597. {
  598. try
  599. {
  600. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  601. if (eventItem == null)
  602. throw new NotFoundException("Мероприятие не найдено", "");
  603.  
  604. var distanceItem = await _uow.Distances.GetByIdAsync(distanceId);
  605. if (distanceItem == null)
  606. throw new NotFoundException("Дистанция не найдена", "");
  607.  
  608. var eventDistances = await _uow.EventDistances.Query()
  609. .Include(e => e.Distance)
  610. .ThenInclude(e => e.DistanceType)
  611. .Include(e => e.Distance)
  612. .ThenInclude(e => e.ParticipationType)
  613. .Where(e => e.EventId == eventId && e.DistanceId == distanceId)
  614. .FirstOrDefaultAsync();
  615.  
  616. var result = _mapper.Map<DistanceViewDTO>(eventDistances);
  617.  
  618. return result;
  619. }
  620. catch (NotFoundException)
  621. {
  622. throw;
  623. }
  624. catch (Exception e)
  625. {
  626. throw new UnexpectedException(e.Message);
  627. }
  628. }
  629.  
  630. public async Task<IList<DistanceViewDTO>> GetEventDistancesAsync(long eventId)
  631. {
  632. try
  633. {
  634. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  635. if (eventItem == null)
  636. throw new NotFoundException("Мероприятие не найдено", "");
  637.  
  638. var eventDistances = await _uow.EventDistances.Query()
  639. .Include(e => e.Distance)
  640. .ThenInclude(e => e.DistanceType)
  641. .Include(e => e.Distance)
  642. .ThenInclude(e => e.ParticipationType)
  643. .Where(e => e.EventId == eventId)
  644. .ToListAsync();
  645.  
  646. var result = _mapper.Map<IList<DistanceViewDTO>>(eventDistances);
  647.  
  648. return result;
  649. }
  650. catch (NotFoundException)
  651. {
  652. throw;
  653. }
  654. catch (Exception e)
  655. {
  656. throw new UnexpectedException(e.Message);
  657. }
  658. }
  659.  
  660. public async Task UpdateEventDistanceAsync(long eventId, long distanceId, DistanceDTO distance, IFormFile file)
  661. {
  662. try
  663. {
  664. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  665. if (eventItem == null)
  666. throw new NotFoundException("Мероприятие не найдено", "");
  667.  
  668. var distanceItem = await _uow.Distances.GetByIdAsync(distanceId);
  669. if (distanceItem == null)
  670. throw new NotFoundException("Дистанция не найдена", "");
  671.  
  672. var eventDistance = await _uow.EventDistances.GetAsync(e => e.EventId == eventId && e.DistanceId == distanceId);
  673. if (eventDistance == null)
  674. throw new NotFoundException("Данная дистанция не принадлежит данному мероприятию", "");
  675.  
  676. string photoPath = distanceItem.DistanceMap;
  677.  
  678. if (file != null)
  679. photoPath = _fileWorkService.PhotoSave(file);
  680.  
  681. _mapper.Map(distance, distanceItem);
  682. distanceItem.Id = distanceId;
  683. distanceItem.DistanceMap = photoPath;
  684.  
  685. _logger.LogInformation($"Mapped entity while updating: {distanceItem.ToString()}");
  686.  
  687. _uow.Distances.Update(distanceItem);
  688.  
  689. var eventParticipationTypes = await _uow.EventParticipantTypes.GetManyAsync(e => e.EventId == eventId);
  690.  
  691. var eventParticipationTypeIds = eventParticipationTypes.Select(e => e.ParticipationTypeId);
  692.  
  693. if (!eventParticipationTypeIds.Contains(distance.ParticipationTypeId))
  694. {
  695. await _uow.EventParticipantTypes.CreateAsync(new EventParticipantType
  696. {
  697. EventId = eventId,
  698. ParticipationTypeId = distance.ParticipationTypeId,
  699. });
  700. }
  701.  
  702. await _uow.SaveChangesAsync();
  703. }
  704. catch (NotFoundException)
  705. {
  706. throw;
  707. }
  708. catch (Exception e)
  709. {
  710. throw new UnexpectedException(e.Message);
  711. }
  712. }
  713.  
  714.  
  715. public async Task UpdateActiveStatusAsync(long eventId, bool isActive)
  716. {
  717. try
  718. {
  719. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  720. if (eventItem == null)
  721. throw new NotFoundException("Мероприятие не найдено", "");
  722.  
  723. eventItem.IsActive = isActive;
  724.  
  725. _uow.Events.Update(eventItem);
  726.  
  727. await _uow.SaveChangesAsync();
  728. }
  729. catch (NotFoundException)
  730. {
  731. throw;
  732. }
  733. catch (Exception e)
  734. {
  735. throw new UnexpectedException(e.Message);
  736. }
  737. }
  738.  
  739. #endregion
  740.  
  741.  
  742. public async Task<IList<DistanceViewDTO>> GetDistancesByParticipationType(long eventId, long participationType)
  743. {
  744. try
  745. {
  746. var eventItem = await _uow.Events.GetByIdAsync(eventId);
  747. if (eventItem == null)
  748. throw new NotFoundException("Мероприятие не найдео", "");
  749.  
  750. var eventDistances = await _uow.EventDistances.Query()
  751. .Include(e => e.Distance)
  752. .ThenInclude(e => e.DistanceType)
  753. .Include(e => e.Distance)
  754. .ThenInclude(e => e.ParticipationType)
  755. .Where(e => e.EventId == eventId && e.Distance.ParticipationTypeId == participationType)
  756. .ToListAsync();
  757.  
  758. if (eventDistances.Count == 0)
  759. {
  760. throw new NotFoundException("Дистанции не найдены", "");
  761. }
  762.  
  763. var result = _mapper.Map<IList<DistanceViewDTO>>(eventDistances);
  764.  
  765. return result;
  766. }
  767. catch (NotFoundException)
  768. {
  769. throw;
  770. }
  771. catch (Exception e)
  772. {
  773. throw new UnexpectedException(e.Message);
  774. }
  775. }
  776.  
  777. public async Task<IList<EventDTO>> GetEventsByParticipationType(long participationType)
  778. {
  779. try
  780. {
  781. var eventIds = await _uow.EventParticipantTypes.Query()
  782. .Where(e => e.ParticipationTypeId == participationType)
  783. .Select(e => e.EventId)
  784. .ToListAsync();
  785.  
  786. var events = await _uow.Events.Query()
  787. .IncludeEventRelatedData()
  788. .Where(e => e.IsActive && eventIds.Contains(e.Id))
  789. .ToListAsync();
  790.  
  791. var result = _mapper.Map<IList<EventDTO>>(events);
  792.  
  793. return result;
  794. }
  795. catch (NotFoundException)
  796. {
  797. throw;
  798. }
  799. catch (Exception e)
  800. {
  801. throw new UnexpectedException(e.Message);
  802. }
  803. }
  804. }
  805. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement