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.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 the newly created item /// If the items is invalid [HttpPost] [ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)] [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()); } [HttpGet] public IEnumerable List() { // list all feedback items. return Enumerable.Range(1, 5).Select(i => new BasicResponse() { Message = "hwlo" } ); } } }