Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using Timerman.Data.Identity;
- using Timerman.Service.DTO.Entities;
- using Timerman.Service.Interfaces;
- using Timerman.Web.Models.BindingModels;
- namespace Timerman.Web.Controllers
- {
- [Route("api/v1")]
- [AllowAnonymous]
- public class EventController : Controller
- {
- private readonly UserManager<ApplicationUser> _userManager;
- private readonly IEventService _eventService;
- private readonly IMapper _mapper;
- private readonly ILogger<EventController> _logger;
- public EventController(UserManager<ApplicationUser> userManager,
- IEventService eventService,
- IMapper mapper,
- ILogger<EventController> logger)
- {
- _userManager = userManager;
- _eventService = eventService;
- _mapper = mapper;
- _logger = logger;
- }
- #region Event
- /// <summary>
- /// Returns info about event by its id
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <returns></returns>
- [HttpGet("events/{eventId}")]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetByIdAsync(long eventId)
- {
- _logger.LogInformation($"GET {nameof(GetByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- var result = await _eventService.EventByIdAsync(eventId);
- _logger.LogInformation($"200 OK : {nameof(GetByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- /// <summary>
- /// Returns results for event by its id.
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <returns></returns>
- [HttpGet("events/{eventId}/result")]
- public async Task<IActionResult> GetResultForEvent(long eventId)
- {
- _logger.LogInformation($"GET {nameof(GetResultForEvent)} by {User.Identity.Name ?? "Anonymous"}.");
- var result = await _eventService.GetResultForEventAsync(eventId);
- _logger.LogInformation($"200 OK : {nameof(GetResultForEvent)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- ///<summary>
- ///Convert eventId to GUID and put to db
- ///</summary>
- ///<param name="eventId">EventId</param>
- ///<returns></returns>
- [HttpGet("eventResults/{eventId}")]
- [Produces("application/json")]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> getEventResultRR(long eventId)
- {
- _logger.LogInformation($"GET {nameof(getEventResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
- //options.RespectBrowserAcceptHeader = true; // false by default
- var result = await _eventService.getEventResultRRAsync(eventId);
- _logger.LogInformation($"200 OK : {nameof(getEventResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- ///<summary>
- ///Get event results by Russia running API
- ///</summary>
- ///<param name="eventId">EventId</param>
- ///<returns></returns>
- [HttpGet("eventsRR/{eventId}")]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetActionResultRR(string eventId)
- {
- _logger.LogInformation($"GET {nameof(GetActionResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
- var result = await _eventService.EventResultRRAsync(eventId);
- _logger.LogInformation($"200 OK : {nameof(GetActionResultRR)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok();
- }
- /// <summary>
- /// Returns info about all active events
- /// </summary>
- /// <returns></returns>
- [HttpGet("events")]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetAllEventAsync()
- {
- _logger.LogInformation($"GET {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- var result = await _eventService.GetAllAsync();
- _logger.LogInformation($"200 OK : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- /// <summary>
- /// Returns info about all events
- /// </summary>
- /// <returns></returns>
- [HttpGet("events-all")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- [ProducesResponseType(404)]
- [ProducesResponseType(403)]
- public async Task<IActionResult> GetAllAdminEventAsync()
- {
- _logger.LogInformation($"GET {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- if (User.Identity.IsAuthenticated)
- {
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (await _userManager.IsInRoleAsync(user, "Employee"))
- {
- var result = await _eventService.GetAllAdminAsync();
- _logger.LogInformation($"200 OK : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- _logger.LogInformation($"403 Forbidden : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- _logger.LogInformation($"401 Unauthorized : {nameof(GetAllEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- /// <summary>
- /// Retutns info about closest event
- /// </summary>
- /// <returns></returns>
- [HttpGet("events/closest")]
- [ProducesResponseType(404)]
- [ProducesResponseType(200)]
- public async Task<IActionResult> GetClosestAsync()
- {
- _logger.LogInformation($"GET {nameof(GetClosestAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- var result = await _eventService.GetClosestAsync();
- _logger.LogInformation($"200 OK : {nameof(GetClosestAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(result);
- }
- /// <summary>
- /// Creates event
- /// </summary>
- /// <param name="model">binding model</param>
- /// <returns></returns>
- [HttpPost("events")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(401)]
- [ProducesResponseType(404)]
- [ProducesResponseType(403)]
- public async Task<IActionResult> Create(EventBindingModel model)
- {
- _logger.LogInformation($"POST {nameof(Create)} event by {User.Identity.Name ?? "Anonymous"}.");
- if (!ModelState.IsValid)
- {
- _logger.LogInformation($"400 BadRequest {nameof(Create)} event.");
- return BadRequest(ModelState);
- }
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(Create)} event.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- return StatusCode(StatusCodes.Status404NotFound);
- _logger.LogDebug($"Find user by name: \"{User.Identity.Name}\".");
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"401 Forbidden {nameof(Create)} event for user {User.Identity.Name}");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- IFormFile file = null;
- IFormFile filePreview = null;
- if (Request.HasFormContentType)
- {
- if (Request.Form.Files.Count > 0)
- {
- var files = Request.Form.Files;
- if (files.Count > 0)
- {
- foreach(var formFile in files)
- {
- if (formFile.Name == "Photo")
- file = formFile;
- if (formFile.Name == "PhotoPreview")
- filePreview = formFile;
- }
- }
- }
- }
- _logger.LogDebug($"Body: {model.ToString()}");
- var dto = _mapper.Map<EventDTO>(model);
- _logger.LogDebug($"DTO: {dto.ToString()}");
- var result = await _eventService.AddEventAsync(dto, user.Id, file, filePreview);
- _logger.LogInformation($"200 Ok {nameof(Create)} event for user {User.Identity.Name}");
- return Ok(result);
- }
- /// <summary>
- /// updates event with id
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <param name="model">binding model</param>
- /// <returns></returns>
- [HttpPut("events/{eventId}")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(401)]
- [ProducesResponseType(404)]
- [ProducesResponseType(403)]
- public async Task<IActionResult> Update(long eventId, EventBindingModel model)
- {
- _logger.LogInformation($"PUT {nameof(Update)} event by {User.Identity.Name ?? "Anonymous"}.");
- if (!ModelState.IsValid)
- {
- _logger.LogInformation($"400 BadRequest {nameof(Update)} event.");
- return BadRequest(ModelState);
- }
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(Update)} event.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- _logger.LogDebug("Update event method started!");
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- return StatusCode(StatusCodes.Status404NotFound);
- _logger.LogDebug($"Find user by name: \"{User.Identity.Name}\".");
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"401 Forbidden {nameof(Update)} event for user {User.Identity.Name}");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- _logger.LogDebug($"Body: {model.ToString()}");
- var dto = _mapper.Map<EventDTO>(model);
- dto.Id = eventId;
- _logger.LogDebug($"DTO: {dto.ToString()}");
- var files = Request.Form.Files;
- IFormFile file = null;
- IFormFile filePreview = null;
- if (files.Count > 0) {
- foreach(var formFile in files)
- {
- if (formFile.Name == "newPhoto")
- file = formFile;
- if (formFile.Name == "newPhotoPreview")
- filePreview = formFile;
- }
- }
- var result = await _eventService.EditEventAsync(eventId, dto, user.Id, file, filePreview);
- _logger.LogInformation($"200 Ok {nameof(Update)} event for user {User.Identity.Name}");
- return Ok(result);
- }
- /// <summary>
- /// deletes event by its id
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <returns></returns>
- [HttpDelete("events/{eventId}")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- [ProducesResponseType(404)]
- [ProducesResponseType(403)]
- public async Task<IActionResult> Delete(long eventId)
- {
- _logger.LogInformation($"DELETE {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
- if (User.Identity.IsAuthenticated)
- {
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (await _userManager.IsInRoleAsync(user, "Employee"))
- {
- await _eventService.DeleteEventAsync(eventId);
- return Ok();
- }
- _logger.LogInformation($"403 Forbid : {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- _logger.LogInformation($"403 Forbid : {nameof(Delete)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- #endregion
- #region event distances
- /// <summary>
- /// returns all event distances for event
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <returns></returns>
- [HttpGet("events/{eventId}/distances")]
- [AllowAnonymous]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetEventDistancesAsync(long eventId)
- {
- _logger.LogInformation($"GET {nameof(GetEventDistancesAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- var res = await _eventService.GetEventDistancesAsync(eventId);
- _logger.LogInformation($"200 OK : {nameof(GetEventDistancesAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(res);
- }
- /// <summary>
- /// returns eventdistance entity
- /// </summary>
- /// <param name="eventId">event identifier</param>
- /// <param name="distanceId">distance identifier</param>
- /// <returns></returns>
- [HttpGet("events/{eventId}/distances/{distanceId}")]
- [AllowAnonymous]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetEventDistanceByIdAsync(long eventId, long distanceId)
- {
- _logger.LogInformation($"GET {nameof(GetEventDistanceByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- var res = await _eventService.GetEventDistanceByIdAsync(eventId, distanceId);
- _logger.LogInformation($"200 OK : {nameof(GetEventDistanceByIdAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(res);
- }
- /// <summary>
- /// saves new distance enitity for event
- /// </summary>
- /// <param name="eventId">event identifier</param>
- /// <param name="distance">distance entity</param>
- /// <returns></returns>
- [HttpPost("events/{eventId}/distances")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(401)]
- [ProducesResponseType(403)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> AddDistanceToEventAsync(long eventId, DistanceBindingModel distance)
- {
- _logger.LogInformation($"POST {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- _logger.LogInformation($"Body: {distance.ToString()}");
- if (!ModelState.IsValid)
- {
- _logger.LogInformation($"400 BadRequest {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return BadRequest(ModelState);
- }
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"403 Forbidden {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- var dto = _mapper.Map<DistanceDTO>(distance);
- _logger.LogInformation($"Mapped: {dto.ToString()}");
- IFormFile file = null;
- if (Request.HasFormContentType)
- {
- if (Request.Form.Files.Count > 0)
- {
- var files = Request.Form.Files;
- if (files.Count == 1)
- {
- file = files[0];
- }
- }
- }
- await _eventService.AddDistanceToEventAsync(eventId, dto, file);
- _logger.LogInformation($"200 OK : {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok();
- }
- /// <summary>
- /// updates specific distance entity
- /// </summary>
- /// <param name="eventId">event identifier</param>
- /// <param name="distanceId">distance identifier</param>
- /// <param name="distance">distance entity</param>
- /// <returns></returns>
- [HttpPut("events/{eventId}/distances/{distanceId}")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(400)]
- [ProducesResponseType(401)]
- [ProducesResponseType(403)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> UpdateEventDistanceAsync(long eventId, long distanceId, DistanceBindingModel distance)
- {
- _logger.LogInformation($"PUT {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- if (!ModelState.IsValid)
- {
- _logger.LogInformation($"400 BadRequest {nameof(AddDistanceToEventAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return BadRequest(ModelState);
- }
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"403 Forbidden {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- var dto = _mapper.Map<DistanceDTO>(distance);
- _logger.LogInformation($"DTO: \n{dto.ToString()}");
- IFormFile file = null;
- var files = Request.Form.Files;
- if (files.Count == 1)
- {
- file = files[0];
- }
- await _eventService.UpdateEventDistanceAsync(eventId, distanceId, dto, file);
- _logger.LogInformation($"200 OK : {nameof(UpdateEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok();
- }
- /// <summary>
- /// deletes event-distance entity
- /// </summary>
- /// <param name="eventId">event identifier</param>
- /// <param name="distanceId">distance identifier</param>
- /// <returns></returns>
- [HttpDelete("events/{eventId}/distances/{distanceId}")]
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- [ProducesResponseType(403)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> DeleteEventDistanceAsync(long eventId, long distanceId)
- {
- _logger.LogInformation($"DELETE {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"403 Forbidden {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- await _eventService.DeleteEventDistanceAsync(eventId, distanceId);
- _logger.LogInformation($"200 OK : {nameof(DeleteEventDistanceAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok();
- }
- #endregion
- [Authorize(Roles = "Employee", AuthenticationSchemes = "Bearer")]
- [HttpPut("events/{eventId}/status")]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- [ProducesResponseType(403)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> UpdateActiveStatusAsync(long eventId, [FromBody] bool isActive)
- {
- _logger.LogInformation($"PUT {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- if (!User.Identity.IsAuthenticated)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- var user = await _userManager.FindByNameAsync(User.Identity.Name);
- if (user == null)
- {
- _logger.LogInformation($"401 Unauthorized {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status401Unauthorized);
- }
- if (! await _userManager.IsInRoleAsync(user, "Employee"))
- {
- _logger.LogInformation($"403 Forbidden {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return StatusCode(StatusCodes.Status403Forbidden);
- }
- await _eventService.UpdateActiveStatusAsync(eventId, isActive);
- _logger.LogInformation($"200 OK : {nameof(UpdateActiveStatusAsync)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok();
- }
- /// <summary>
- /// returns distances for event with specific participation type
- /// </summary>
- /// <param name="eventId">event id</param>
- /// <param name="participationTypeId">participation type</param>
- /// <remarks>
- /// ParticipationTypes
- /// 1 - individual
- /// 2 - team
- /// 3 - kids
- /// </remarks>
- /// <returns></returns>
- [HttpGet("events/{eventId}/participation-types/{participationTypeId}/distances")]
- [AllowAnonymous]
- [ProducesResponseType(200)]
- [ProducesResponseType(401)]
- [ProducesResponseType(403)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetDistancesByParticipationType(long eventId, long participationTypeId)
- {
- _logger.LogInformation($"GET {nameof(GetDistancesByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
- var res = await _eventService.GetDistancesByParticipationType(eventId, participationTypeId);
- _logger.LogInformation($"200 OK {nameof(GetDistancesByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(res);
- }
- [HttpGet("events/participation-types/{participationTypeId}")]
- [AllowAnonymous]
- [ProducesResponseType(200)]
- [ProducesResponseType(404)]
- public async Task<IActionResult> GetEventsByParticipationType(long participationTypeId)
- {
- _logger.LogInformation($"GET {nameof(GetEventsByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
- var res = await _eventService.GetEventsByParticipationType(participationTypeId);
- _logger.LogInformation($"200 OK {nameof(GetEventsByParticipationType)} by {User.Identity.Name ?? "Anonymous"}.");
- return Ok(res);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement