2021-05-29 17:02:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-05-29 18:12:36 +00:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoMapper;
|
2021-05-29 18:12:36 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-05-29 18:12:36 +00:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
using Retroactiune.Models;
|
|
|
|
|
using Retroactiune.Services;
|
|
|
|
|
|
|
|
|
|
namespace Retroactiune.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/v1/[controller]")]
|
|
|
|
|
public class FeedbackReceiverController : ControllerBase
|
|
|
|
|
{
|
2021-05-29 18:12:36 +00:00
|
|
|
|
private readonly IOptions<ApiBehaviorOptions> _apiBehaviorOptions;
|
|
|
|
|
private readonly IFeedbackReceiverService _service;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
2021-05-29 18:12:36 +00:00
|
|
|
|
public FeedbackReceiverController(IFeedbackReceiverService service, IMapper mapper,
|
|
|
|
|
IOptions<ApiBehaviorOptions> apiBehaviorOptions)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
_service = service;
|
|
|
|
|
_mapper = mapper;
|
2021-05-29 18:12:36 +00:00
|
|
|
|
_apiBehaviorOptions = apiBehaviorOptions;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-29 18:12:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Inserts FeedbackReceivers into the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">The list of FeedbackReceivers</param>
|
|
|
|
|
/// <returns>A BasicResponse indicating success.</returns>
|
|
|
|
|
/// <response code="201">Returns the newly created item</response>
|
|
|
|
|
/// <response code="400">If the items is invalid</response>
|
2021-05-29 17:02:16 +00:00
|
|
|
|
[HttpPost]
|
2021-05-29 18:12:36 +00:00
|
|
|
|
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status201Created)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Post([Required] IEnumerable<FeedbackReceiverDto> items)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-05-29 18:12:36 +00:00
|
|
|
|
var feedbackReceiversDto = items.ToList();
|
|
|
|
|
if (!feedbackReceiversDto.Any())
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(IEnumerable<FeedbackReceiverDto>),
|
|
|
|
|
"At least one FeedbackReceiver item is required.");
|
|
|
|
|
return _apiBehaviorOptions?.Value.InvalidModelStateResponseFactory(ControllerContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mappedItems = feedbackReceiversDto.Select(i =>
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
var result = _mapper.Map<FeedbackReceiver>(i);
|
|
|
|
|
result.CreatedAt = DateTime.UtcNow;
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-29 18:12:36 +00:00
|
|
|
|
await _service.CreateManyAsync(mappedItems);
|
2021-05-29 17:02:16 +00:00
|
|
|
|
|
2021-05-29 18:12:36 +00:00
|
|
|
|
return Ok(new BasicResponse()
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
Message = "Items created successfully!"
|
2021-05-29 18:12:36 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public NoContentResult Delete(long id)
|
|
|
|
|
{
|
|
|
|
|
// delete feedback item.
|
|
|
|
|
return NoContent();
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public BasicResponse Get(long id)
|
|
|
|
|
{
|
|
|
|
|
// get feedback item from db
|
|
|
|
|
return new BasicResponse()
|
|
|
|
|
{
|
|
|
|
|
Message = "hwlo"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IEnumerable<BasicResponse> List()
|
|
|
|
|
{
|
|
|
|
|
// list all feedback items.
|
|
|
|
|
return Enumerable.Range(1, 5).Select(i =>
|
|
|
|
|
new BasicResponse()
|
|
|
|
|
{
|
|
|
|
|
Message = "hwlo"
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|