Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Identity;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.Extensions.Logging;
  10. using Timerman.Data.Identity;
  11. using Timerman.Service.DTO.Entities;
  12. using Timerman.Service.Interfaces;
  13. using Timerman.Web.Models.BindingModels;
  14.  
  15.  
  16.  
  17. namespace Timerman.Web.Controllers
  18. {
  19. [Route("api/v1")]
  20. [AllowAnonymous]
  21. public class EventController : Controller
  22. {
  23. private readonly UserManager<ApplicationUser> _userManager;
  24. private readonly IEventService _eventService;
  25. private readonly IMapper _mapper;
  26. private readonly ILogger<EventController> _logger;
  27.  
  28.  
  29. public EventController(UserManager<ApplicationUser> userManager,
  30. IEventService eventService,
  31. IMapper mapper,
  32. ILogger<EventController> logger)
  33. {
  34. _userManager = userManager;
  35. _eventService = eventService;
  36. _mapper = mapper;
  37. _logger = logger;
  38. }
  39.  
  40. #region Event
  41.  
  42. /// <summary>
  43. /// Returns info about event by its id
  44. /// </summary>
  45. /// <param name="eventId">event id</param>
  46. /// <returns></returns>
  47. [HttpGet("events/{eventId}")]
  48. [ProducesResponseType(200)]
  49. [ProducesResponseType(404)]
  50. public async Task<IActionResult> GetByIdAsync(long eventId)
  51. {
  52. _logger.LogInformation($"GET {nameof(GetByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  53.  
  54. var result = await _eventService.EventByIdAsync(eventId);
  55.  
  56. _logger.LogInformation($"200 OK : {nameof(GetByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  57. return Ok(result);
  58. }
  59.  
  60. /// <summary>
  61. /// Returns results for event by its id.
  62. /// </summary>
  63. /// <param name="eventId">event id</param>
  64. /// <returns></returns>
  65. [HttpGet("events/{eventId}/result")]
  66. public async Task<IActionResult> GetResultForEvent(long eventId)
  67. {
  68. _logger.LogInformation($"GET {nameof(GetResultForEvent)} by {User.Identity.Name ?? "Anonymous"}.");
  69.  
  70. var result = await _eventService.GetResultForEventAsync(eventId);
  71.  
  72. _logger.LogInformation($"200 OK : {nameof(GetResultForEvent)} by {User.Identity.Name ?? "Anonymous"}.");
  73. return Ok(result);
  74. }
  75.  
  76. ///<summary>
  77. ///Convert eventId to GUID and put to db
  78. ///</summary>
  79. ///<param name="eventId">EventId</param>
  80. ///<returns></returns>
  81. [HttpGet("eventResults/{eventId}")]
  82. [Produces("application/json")]
  83. [ProducesResponseType(200)]
  84. [ProducesResponseType(404)]
  85. public async Task<IActionResult> getEventResultRR(long eventId)
  86. {
  87. _logger.LogInformation($"GET {nameof(getEventResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
  88.  
  89. //options.RespectBrowserAcceptHeader = true; // false by default
  90.  
  91. var result = await _eventService.getEventResultRRAsync(eventId);
  92.  
  93. _logger.LogInformation($"200 OK : {nameof(getEventResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
  94.  
  95. return Ok(result);
  96. }
  97.  
  98. ///<summary>
  99. ///Get event results by Russia running API
  100. ///</summary>
  101. ///<param name="eventId">EventId</param>
  102. ///<returns></returns>
  103. [HttpGet("eventsRR/{eventId}")]
  104. [ProducesResponseType(200)]
  105. [ProducesResponseType(404)]
  106. public async Task<IActionResult> GetActionResultRR(string eventId)
  107. {
  108. _logger.LogInformation($"GET {nameof(GetActionResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
  109.  
  110. var result = await _eventService.EventResultRRAsync(eventId);
  111.  
  112. _logger.LogInformation($"200 OK : {nameof(GetActionResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
  113.  
  114. return Ok();
  115. }
  116.  
  117. /// <summary>
  118. /// Returns info about all active events
  119. /// </summary>
  120. /// <returns></returns>
  121. [HttpGet("events")]
  122. [ProducesResponseType(200)]
  123. [ProducesResponseType(404)]
  124. public async Task<IActionResult> GetAllEventAsync()
  125. {
  126. _logger.LogInformation($"GET {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  127.  
  128. var result = await _eventService.GetAllAsync();
  129.  
  130. _logger.LogInformation($"200 OK : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  131.  
  132. return Ok(result);
  133. }
  134.  
  135. /// <summary>
  136. /// Returns info about all events
  137. /// </summary>
  138. /// <returns></returns>
  139. [HttpGet("events-all")]
  140. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  141. [ProducesResponseType(200)]
  142. [ProducesResponseType(401)]
  143. [ProducesResponseType(404)]
  144. [ProducesResponseType(403)]
  145. public async Task<IActionResult> GetAllAdminEventAsync()
  146. {
  147. _logger.LogInformation($"GET {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  148.  
  149. if (User.Identity.IsAuthenticated)
  150. {
  151. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  152.  
  153. if (await _userManager.IsInRoleAsync(user, "Employee"))
  154. {
  155. var result = await _eventService.GetAllAdminAsync();
  156.  
  157. _logger.LogInformation($"200 OK : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  158.  
  159. return Ok(result);
  160. }
  161. _logger.LogInformation($"403 Forbidden : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  162. return StatusCode(StatusCodes.Status403Forbidden);
  163. }
  164. _logger.LogInformation($"401 Unauthorized : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  165. return StatusCode(StatusCodes.Status401Unauthorized);
  166. }
  167. /// <summary>
  168. /// Retutns info about closest event
  169. /// </summary>
  170. /// <returns></returns>
  171. [HttpGet("events/closest")]
  172. [ProducesResponseType(404)]
  173. [ProducesResponseType(200)]
  174. public async Task<IActionResult> GetClosestAsync()
  175. {
  176. _logger.LogInformation($"GET {nameof(GetClosestAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  177.  
  178. var result = await _eventService.GetClosestAsync();
  179.  
  180. _logger.LogInformation($"200 OK : {nameof(GetClosestAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  181.  
  182. return Ok(result);
  183. }
  184.  
  185. /// <summary>
  186. /// Creates event
  187. /// </summary>
  188. /// <param name="model">binding model</param>
  189. /// <returns></returns>
  190. [HttpPost("events")]
  191. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  192. [ProducesResponseType(200)]
  193. [ProducesResponseType(400)]
  194. [ProducesResponseType(401)]
  195. [ProducesResponseType(404)]
  196. [ProducesResponseType(403)]
  197. public async Task<IActionResult> Create(EventBindingModel model)
  198. {
  199. _logger.LogInformation($"POST {nameof(Create)} event by {User.Identity.Name ?? "Anonymous"}.");
  200. if (!ModelState.IsValid)
  201. {
  202. _logger.LogInformation($"400 BadRequest {nameof(Create)} event.");
  203. return BadRequest(ModelState);
  204. }
  205.  
  206. if (!User.Identity.IsAuthenticated)
  207. {
  208. _logger.LogInformation($"401 Unauthorized {nameof(Create)} event.");
  209. return StatusCode(StatusCodes.Status401Unauthorized);
  210. }
  211.  
  212. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  213.  
  214. if (user == null)
  215. return StatusCode(StatusCodes.Status404NotFound);
  216.  
  217. _logger.LogDebug($"Find user by name: \"{User.Identity.Name}\".");
  218.  
  219. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  220. {
  221. _logger.LogInformation($"401 Forbidden {nameof(Create)} event for user {User.Identity.Name}");
  222. return StatusCode(StatusCodes.Status403Forbidden);
  223. }
  224.  
  225. IFormFile file = null;
  226. IFormFile filePreview = null;
  227.  
  228. if (Request.HasFormContentType)
  229. {
  230. if (Request.Form.Files.Count > 0)
  231. {
  232. var files = Request.Form.Files;
  233.  
  234. if (files.Count > 0)
  235. {
  236. foreach(var formFile in files)
  237. {
  238. if (formFile.Name == "Photo")
  239. file = formFile;
  240. if (formFile.Name == "PhotoPreview")
  241. filePreview = formFile;
  242. }
  243. }
  244. }
  245. }
  246.  
  247. _logger.LogDebug($"Body: {model.ToString()}");
  248.  
  249. var dto = _mapper.Map<EventDTO>(model);
  250.  
  251. _logger.LogDebug($"DTO: {dto.ToString()}");
  252.  
  253. var result = await _eventService.AddEventAsync(dto, user.Id, file, filePreview);
  254.  
  255. _logger.LogInformation($"200 Ok {nameof(Create)} event for user {User.Identity.Name}");
  256.  
  257. return Ok(result);
  258. }
  259.  
  260.  
  261.  
  262. /// <summary>
  263. /// updates event with id
  264. /// </summary>
  265. /// <param name="eventId">event id</param>
  266. /// <param name="model">binding model</param>
  267. /// <returns></returns>
  268. [HttpPut("events/{eventId}")]
  269. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  270. [ProducesResponseType(200)]
  271. [ProducesResponseType(400)]
  272. [ProducesResponseType(401)]
  273. [ProducesResponseType(404)]
  274. [ProducesResponseType(403)]
  275. public async Task<IActionResult> Update(long eventId, EventBindingModel model)
  276. {
  277. _logger.LogInformation($"PUT {nameof(Update)} event by {User.Identity.Name ?? "Anonymous"}.");
  278. if (!ModelState.IsValid)
  279. {
  280. _logger.LogInformation($"400 BadRequest {nameof(Update)} event.");
  281. return BadRequest(ModelState);
  282. }
  283.  
  284. if (!User.Identity.IsAuthenticated)
  285. {
  286. _logger.LogInformation($"401 Unauthorized {nameof(Update)} event.");
  287. return StatusCode(StatusCodes.Status401Unauthorized);
  288. }
  289.  
  290. _logger.LogDebug("Update event method started!");
  291.  
  292. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  293.  
  294. if (user == null)
  295. return StatusCode(StatusCodes.Status404NotFound);
  296.  
  297. _logger.LogDebug($"Find user by name: \"{User.Identity.Name}\".");
  298.  
  299. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  300. {
  301. _logger.LogInformation($"401 Forbidden {nameof(Update)} event for user {User.Identity.Name}");
  302. return StatusCode(StatusCodes.Status403Forbidden);
  303. }
  304.  
  305. _logger.LogDebug($"Body: {model.ToString()}");
  306.  
  307. var dto = _mapper.Map<EventDTO>(model);
  308. dto.Id = eventId;
  309.  
  310. _logger.LogDebug($"DTO: {dto.ToString()}");
  311.  
  312. var files = Request.Form.Files;
  313. IFormFile file = null;
  314. IFormFile filePreview = null;
  315. if (files.Count > 0) {
  316. foreach(var formFile in files)
  317. {
  318. if (formFile.Name == "newPhoto")
  319. file = formFile;
  320. if (formFile.Name == "newPhotoPreview")
  321. filePreview = formFile;
  322. }
  323. }
  324.  
  325. var result = await _eventService.EditEventAsync(eventId, dto, user.Id, file, filePreview);
  326.  
  327. _logger.LogInformation($"200 Ok {nameof(Update)} event for user {User.Identity.Name}");
  328.  
  329. return Ok(result);
  330. }
  331.  
  332. /// <summary>
  333. /// deletes event by its id
  334. /// </summary>
  335. /// <param name="eventId">event id</param>
  336. /// <returns></returns>
  337. [HttpDelete("events/{eventId}")]
  338. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  339. [ProducesResponseType(200)]
  340. [ProducesResponseType(401)]
  341. [ProducesResponseType(404)]
  342. [ProducesResponseType(403)]
  343. public async Task<IActionResult> Delete(long eventId)
  344. {
  345. _logger.LogInformation($"DELETE {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
  346.  
  347. if (User.Identity.IsAuthenticated)
  348. {
  349. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  350.  
  351. if (await _userManager.IsInRoleAsync(user, "Employee"))
  352. {
  353. await _eventService.DeleteEventAsync(eventId);
  354.  
  355. return Ok();
  356. }
  357. _logger.LogInformation($"403 Forbid : {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
  358. return StatusCode(StatusCodes.Status401Unauthorized);
  359. }
  360. _logger.LogInformation($"403 Forbid : {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
  361. return StatusCode(StatusCodes.Status401Unauthorized);
  362.  
  363. }
  364.  
  365. #endregion
  366.  
  367. #region event distances
  368. /// <summary>
  369. /// returns all event distances for event
  370. /// </summary>
  371. /// <param name="eventId">event id</param>
  372. /// <returns></returns>
  373. [HttpGet("events/{eventId}/distances")]
  374. [AllowAnonymous]
  375. [ProducesResponseType(200)]
  376. [ProducesResponseType(404)]
  377. public async Task<IActionResult> GetEventDistancesAsync(long eventId)
  378. {
  379. _logger.LogInformation($"GET {nameof(GetEventDistancesAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  380. var res = await _eventService.GetEventDistancesAsync(eventId);
  381.  
  382. _logger.LogInformation($"200 OK : {nameof(GetEventDistancesAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  383. return Ok(res);
  384. }
  385.  
  386. /// <summary>
  387. /// returns eventdistance entity
  388. /// </summary>
  389. /// <param name="eventId">event identifier</param>
  390. /// <param name="distanceId">distance identifier</param>
  391. /// <returns></returns>
  392. [HttpGet("events/{eventId}/distances/{distanceId}")]
  393. [AllowAnonymous]
  394. [ProducesResponseType(200)]
  395. [ProducesResponseType(404)]
  396. public async Task<IActionResult> GetEventDistanceByIdAsync(long eventId, long distanceId)
  397. {
  398. _logger.LogInformation($"GET {nameof(GetEventDistanceByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  399. var res = await _eventService.GetEventDistanceByIdAsync(eventId, distanceId);
  400.  
  401. _logger.LogInformation($"200 OK : {nameof(GetEventDistanceByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  402. return Ok(res);
  403. }
  404.  
  405. /// <summary>
  406. /// saves new distance enitity for event
  407. /// </summary>
  408. /// <param name="eventId">event identifier</param>
  409. /// <param name="distance">distance entity</param>
  410. /// <returns></returns>
  411. [HttpPost("events/{eventId}/distances")]
  412. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  413. [ProducesResponseType(200)]
  414. [ProducesResponseType(400)]
  415. [ProducesResponseType(401)]
  416. [ProducesResponseType(403)]
  417. [ProducesResponseType(404)]
  418. public async Task<IActionResult> AddDistanceToEventAsync(long eventId, DistanceBindingModel distance)
  419. {
  420.  
  421. _logger.LogInformation($"POST {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  422.  
  423. _logger.LogInformation($"Body: {distance.ToString()}");
  424.  
  425. if (!ModelState.IsValid)
  426. {
  427. _logger.LogInformation($"400 BadRequest {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  428. return BadRequest(ModelState);
  429. }
  430.  
  431. if (!User.Identity.IsAuthenticated)
  432. {
  433. _logger.LogInformation($"401 Unauthorized {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  434. return StatusCode(StatusCodes.Status401Unauthorized);
  435. }
  436.  
  437. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  438.  
  439. if (user == null)
  440. {
  441. _logger.LogInformation($"401 Unauthorized {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  442. return StatusCode(StatusCodes.Status401Unauthorized);
  443. }
  444.  
  445. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  446. {
  447. _logger.LogInformation($"403 Forbidden {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  448. return StatusCode(StatusCodes.Status403Forbidden);
  449. }
  450.  
  451. var dto = _mapper.Map<DistanceDTO>(distance);
  452.  
  453. _logger.LogInformation($"Mapped: {dto.ToString()}");
  454.  
  455. IFormFile file = null;
  456. if (Request.HasFormContentType)
  457. {
  458. if (Request.Form.Files.Count > 0)
  459. {
  460. var files = Request.Form.Files;
  461. if (files.Count == 1)
  462. {
  463. file = files[0];
  464. }
  465. }
  466. }
  467.  
  468. await _eventService.AddDistanceToEventAsync(eventId, dto, file);
  469.  
  470. _logger.LogInformation($"200 OK : {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  471.  
  472. return Ok();
  473. }
  474.  
  475. /// <summary>
  476. /// updates specific distance entity
  477. /// </summary>
  478. /// <param name="eventId">event identifier</param>
  479. /// <param name="distanceId">distance identifier</param>
  480. /// <param name="distance">distance entity</param>
  481. /// <returns></returns>
  482. [HttpPut("events/{eventId}/distances/{distanceId}")]
  483. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  484. [ProducesResponseType(200)]
  485. [ProducesResponseType(400)]
  486. [ProducesResponseType(401)]
  487. [ProducesResponseType(403)]
  488. [ProducesResponseType(404)]
  489. public async Task<IActionResult> UpdateEventDistanceAsync(long eventId, long distanceId, DistanceBindingModel distance)
  490. {
  491. _logger.LogInformation($"PUT {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  492.  
  493. if (!ModelState.IsValid)
  494. {
  495. _logger.LogInformation($"400 BadRequest {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  496. return BadRequest(ModelState);
  497. }
  498.  
  499. if (!User.Identity.IsAuthenticated)
  500. {
  501. _logger.LogInformation($"401 Unauthorized {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  502. return StatusCode(StatusCodes.Status401Unauthorized);
  503. }
  504.  
  505. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  506.  
  507. if (user == null)
  508. {
  509. _logger.LogInformation($"401 Unauthorized {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  510. return StatusCode(StatusCodes.Status401Unauthorized);
  511. }
  512.  
  513. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  514. {
  515. _logger.LogInformation($"403 Forbidden {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  516. return StatusCode(StatusCodes.Status403Forbidden);
  517. }
  518.  
  519.  
  520. var dto = _mapper.Map<DistanceDTO>(distance);
  521. _logger.LogInformation($"DTO: \n{dto.ToString()}");
  522.  
  523. IFormFile file = null;
  524. var files = Request.Form.Files;
  525. if (files.Count == 1)
  526. {
  527. file = files[0];
  528. }
  529.  
  530. await _eventService.UpdateEventDistanceAsync(eventId, distanceId, dto, file);
  531.  
  532. _logger.LogInformation($"200 OK : {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  533.  
  534. return Ok();
  535. }
  536.  
  537. /// <summary>
  538. /// deletes event-distance entity
  539. /// </summary>
  540. /// <param name="eventId">event identifier</param>
  541. /// <param name="distanceId">distance identifier</param>
  542. /// <returns></returns>
  543. [HttpDelete("events/{eventId}/distances/{distanceId}")]
  544. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  545. [ProducesResponseType(200)]
  546. [ProducesResponseType(401)]
  547. [ProducesResponseType(403)]
  548. [ProducesResponseType(404)]
  549. public async Task<IActionResult> DeleteEventDistanceAsync(long eventId, long distanceId)
  550. {
  551. _logger.LogInformation($"DELETE {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  552.  
  553. if (!User.Identity.IsAuthenticated)
  554. {
  555. _logger.LogInformation($"401 Unauthorized {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  556. return StatusCode(StatusCodes.Status401Unauthorized);
  557. }
  558.  
  559. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  560.  
  561. if (user == null)
  562. {
  563. _logger.LogInformation($"401 Unauthorized {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  564. return StatusCode(StatusCodes.Status401Unauthorized);
  565. }
  566.  
  567. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  568. {
  569. _logger.LogInformation($"403 Forbidden {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  570. return StatusCode(StatusCodes.Status403Forbidden);
  571. }
  572.  
  573. await _eventService.DeleteEventDistanceAsync(eventId, distanceId);
  574.  
  575. _logger.LogInformation($"200 OK : {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  576.  
  577. return Ok();
  578. }
  579. #endregion
  580.  
  581. [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
  582. [HttpPut("events/{eventId}/status")]
  583. [ProducesResponseType(200)]
  584. [ProducesResponseType(401)]
  585. [ProducesResponseType(403)]
  586. [ProducesResponseType(404)]
  587. public async Task<IActionResult> UpdateActiveStatusAsync(long eventId, [FromBody] bool isActive)
  588. {
  589. _logger.LogInformation($"PUT {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  590.  
  591. if (!User.Identity.IsAuthenticated)
  592. {
  593. _logger.LogInformation($"401 Unauthorized {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  594. return StatusCode(StatusCodes.Status401Unauthorized);
  595. }
  596.  
  597. var user = await _userManager.FindByNameAsync(User.Identity.Name);
  598.  
  599. if (user == null)
  600. {
  601. _logger.LogInformation($"401 Unauthorized {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  602. return StatusCode(StatusCodes.Status401Unauthorized);
  603. }
  604.  
  605. if (! await _userManager.IsInRoleAsync(user, "Employee"))
  606. {
  607. _logger.LogInformation($"403 Forbidden {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  608. return StatusCode(StatusCodes.Status403Forbidden);
  609. }
  610. await _eventService.UpdateActiveStatusAsync(eventId, isActive);
  611.  
  612. _logger.LogInformation($"200 OK : {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
  613. return Ok();
  614. }
  615.  
  616. /// <summary>
  617. /// returns distances for event with specific participation type
  618. /// </summary>
  619. /// <param name="eventId">event id</param>
  620. /// <param name="participationTypeId">participation type</param>
  621. /// <remarks>
  622. /// ParticipationTypes
  623. /// 1 - individual
  624. /// 2 - team
  625. /// 3 - kids
  626. /// </remarks>
  627. /// <returns></returns>
  628. [HttpGet("events/{eventId}/participation-types/{participationTypeId}/distances")]
  629. [AllowAnonymous]
  630. [ProducesResponseType(200)]
  631. [ProducesResponseType(401)]
  632. [ProducesResponseType(403)]
  633. [ProducesResponseType(404)]
  634. public async Task<IActionResult> GetDistancesByParticipationType(long eventId, long participationTypeId)
  635. {
  636. _logger.LogInformation($"GET {nameof(GetDistancesByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
  637.  
  638. var res = await _eventService.GetDistancesByParticipationType(eventId, participationTypeId);
  639.  
  640. _logger.LogInformation($"200 OK {nameof(GetDistancesByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
  641.  
  642. return Ok(res);
  643. }
  644.  
  645. [HttpGet("events/participation-types/{participationTypeId}")]
  646. [AllowAnonymous]
  647. [ProducesResponseType(200)]
  648. [ProducesResponseType(404)]
  649. public async Task<IActionResult> GetEventsByParticipationType(long participationTypeId)
  650. {
  651. _logger.LogInformation($"GET {nameof(GetEventsByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
  652.  
  653. var res = await _eventService.GetEventsByParticipationType(participationTypeId);
  654.  
  655. _logger.LogInformation($"200 OK {nameof(GetEventsByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
  656.  
  657. return Ok(res);
  658. }
  659. }
  660. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement