using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; using AutoMapper; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Retroactiune.DataTransferObjects; using Retroactiune.Models; using Retroactiune.Services; namespace Retroactiune.Controllers { [ApiController] [Route("api/v1/[controller]")] public class FeedbackReceiversController : ControllerBase { private readonly IOptions _apiBehaviorOptions; private readonly IFeedbackReceiverService _service; private readonly IMapper _mapper; public FeedbackReceiversController(IFeedbackReceiverService service, IMapper mapper, IOptions apiBehaviorOptions) { _service = service; _mapper = mapper; _apiBehaviorOptions = apiBehaviorOptions; } /// /// Inserts FeedbackReceiver items into the database. /// /// The list of FeedbackReceivers /// A BasicResponse indicating success. /// Returns an ok message. /// If the items is invalid [HttpPost] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Post([Required] IEnumerable items) { var feedbackReceiversDto = items.ToList(); if (!feedbackReceiversDto.Any()) { ModelState.AddModelError(nameof(IEnumerable), "At least one FeedbackReceiver item is required."); return _apiBehaviorOptions?.Value.InvalidModelStateResponseFactory(ControllerContext); } var mappedItems = feedbackReceiversDto.Select(i => { var result = _mapper.Map(i); result.CreatedAt = DateTime.UtcNow; return result; }); await _service.CreateManyAsync(mappedItems); return Ok(new BasicResponse() { Message = "Items created successfully!" }); } /// /// Deletes a FeedbackReceiver item from the database. /// /// The guid of the item to be deleted. /// A NoContent result. /// The delete is successful. /// The delete is unsuccessful. [HttpDelete("{guid}")] [ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Delete( [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] string guid) { await _service.DeleteOneAsync(guid); return NoContent(); } /// /// Retrieves a FeedbackReceiver item from the database. /// /// The guid of the item to be retrieved. /// A Ok result with a . /// The item returned successfully. /// The request is invalid. /// The item was not found. [HttpGet("{guid}")] [ProducesResponseType(typeof(FeedbackReceiverOutDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task Get( [StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)] string guid) { var result = await _service.FindAsync(new[] {guid}); var feedbackReceivers = result as FeedbackReceiver[] ?? result.ToArray(); if (!feedbackReceivers.Any()) { return NotFound(new BasicResponse() { Message = $"Item with guid {guid} was not found." }); } return Ok(feedbackReceivers.First()); } /// /// Retrieves a FeedbackReceiver items from the database. /// /// If set, it will filter results for the given guids. /// If set, it will skip the N items. Allowed range is 1-IntMax. /// If set, it will limit the results to N items. Allowed range is 1-1000. /// A Ok result with a list of . /// The a list is returned. /// The request is invalid. [HttpGet] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task List([FromQuery] IEnumerable filter, [RangeAttribute(1, int.MaxValue, ErrorMessage = "offset is out of range, allowed ranges [1-IntMax]"), FromQuery] int offset, [RangeAttribute(1, 1000, ErrorMessage = "limit is out of range, allowed ranges [1-1000]"), FromQuery] int limit) { return Ok(await _service.FindAsync(filter, offset, limit)); } } }