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-06-09 21:03:07 +00:00
|
|
|
|
using Retroactiune.DataTransferObjects;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
using Retroactiune.Models;
|
|
|
|
|
using Retroactiune.Services;
|
|
|
|
|
|
|
|
|
|
namespace Retroactiune.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/v1/[controller]")]
|
2021-06-01 13:20:31 +00:00
|
|
|
|
public class FeedbackReceiversController : ControllerBase
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
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-06-01 13:20:31 +00:00
|
|
|
|
public FeedbackReceiversController(IFeedbackReceiverService service, IMapper mapper,
|
2021-05-29 18:12:36 +00:00
|
|
|
|
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>
|
2021-06-01 13:22:38 +00:00
|
|
|
|
/// Inserts FeedbackReceiver items into the database.
|
2021-05-29 18:12:36 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">The list of FeedbackReceivers</param>
|
|
|
|
|
/// <returns>A BasicResponse indicating success.</returns>
|
2021-06-09 21:03:07 +00:00
|
|
|
|
/// <response code="200">Returns an ok message.</response>
|
2021-05-29 18:12:36 +00:00
|
|
|
|
/// <response code="400">If the items is invalid</response>
|
2021-05-29 17:02:16 +00:00
|
|
|
|
[HttpPost]
|
2021-06-09 21:03:07 +00:00
|
|
|
|
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status200OK)]
|
2021-05-29 18:12:36 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2021-06-05 13:03:12 +00:00
|
|
|
|
public async Task<IActionResult> Post([Required] IEnumerable<FeedbackReceiverInDto> items)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-05-29 18:12:36 +00:00
|
|
|
|
var feedbackReceiversDto = items.ToList();
|
|
|
|
|
if (!feedbackReceiversDto.Any())
|
|
|
|
|
{
|
2021-06-05 13:03:12 +00:00
|
|
|
|
ModelState.AddModelError(nameof(IEnumerable<FeedbackReceiverInDto>),
|
2021-05-29 18:12:36 +00:00
|
|
|
|
"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
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 13:22:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a FeedbackReceiver item from the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guid">The guid of the item to be deleted.</param>
|
|
|
|
|
/// <returns>A NoContent result.</returns>
|
2021-06-20 11:25:04 +00:00
|
|
|
|
/// <response code="204">The delete is submitted.</response>
|
|
|
|
|
/// <response code="400">The request is invalid.</response>
|
2021-06-01 13:20:31 +00:00
|
|
|
|
[HttpDelete("{guid}")]
|
2021-06-01 13:22:38 +00:00
|
|
|
|
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2021-06-01 13:20:31 +00:00
|
|
|
|
public async Task<NoContentResult> Delete(
|
|
|
|
|
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
|
|
|
|
|
string guid)
|
2021-05-29 18:12:36 +00:00
|
|
|
|
{
|
2021-06-20 11:25:04 +00:00
|
|
|
|
await _service.DeleteManyAsync(new [] {guid});
|
2021-05-29 18:12:36 +00:00
|
|
|
|
return NoContent();
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 13:03:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a FeedbackReceiver item from the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="guid">The guid of the item to be retrieved.</param>
|
|
|
|
|
/// <returns>A Ok result with a <see cref="FeedbackReceiverOutDto"/>.</returns>
|
|
|
|
|
/// <response code="200">The item returned successfully.</response>
|
|
|
|
|
/// <response code="400">The request is invalid.</response>
|
|
|
|
|
/// <response code="404">The item was not found.</response>
|
|
|
|
|
[HttpGet("{guid}")]
|
|
|
|
|
[ProducesResponseType(typeof(FeedbackReceiverOutDto), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(BasicResponse), StatusCodes.Status404NotFound)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> Get(
|
|
|
|
|
[StringLength(24, ErrorMessage = "invalid guid, must be 24 characters", MinimumLength = 24)]
|
|
|
|
|
string guid)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-06-05 13:03:12 +00:00
|
|
|
|
var result = await _service.FindAsync(new[] {guid});
|
2021-06-05 14:47:22 +00:00
|
|
|
|
var feedbackReceivers = result as FeedbackReceiver[] ?? result.ToArray();
|
|
|
|
|
if (!feedbackReceivers.Any())
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-06-05 13:03:12 +00:00
|
|
|
|
return NotFound(new BasicResponse()
|
|
|
|
|
{
|
|
|
|
|
Message = $"Item with guid {guid} was not found."
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 14:47:22 +00:00
|
|
|
|
return Ok(feedbackReceivers.First());
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 16:39:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves a FeedbackReceiver items from the database.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filter">If set, it will filter results for the given guids.</param>
|
2021-06-05 19:54:56 +00:00
|
|
|
|
/// <param name="offset">If set, it will skip the N items. Allowed range is 1-IntMax.</param>
|
|
|
|
|
/// <param name="limit">If set, it will limit the results to N items. Allowed range is 1-1000.</param>
|
2021-06-05 16:39:47 +00:00
|
|
|
|
/// <returns>A Ok result with a list of <see cref="FeedbackReceiverOutDto"/>.</returns>
|
|
|
|
|
/// <response code="200">The a list is returned.</response>
|
|
|
|
|
/// <response code="400">The request is invalid.</response>
|
2021-05-29 17:02:16 +00:00
|
|
|
|
[HttpGet]
|
2021-06-05 16:39:47 +00:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<FeedbackReceiverOutDto>), StatusCodes.Status200OK)]
|
2021-06-05 19:54:56 +00:00
|
|
|
|
public async Task<IActionResult> List([FromQuery] IEnumerable<string> 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)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-06-05 16:39:47 +00:00
|
|
|
|
return Ok(await _service.FindAsync(filter, offset, limit));
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
2021-06-20 11:25:04 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes FeedbackReceiver identified by ids.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids">A list of FeedbackReceiver ids.</param>
|
|
|
|
|
/// <response code="204">The request to delete the items has been submitted.</response>
|
|
|
|
|
/// <response code="404">The request is invalid.</response>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[ProducesResponseType(typeof(NoContentResult), StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(typeof(BasicResponse),StatusCodes.Status400BadRequest)]
|
|
|
|
|
public async Task<IActionResult> DeleteTokens([Required] IEnumerable<string> ids)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Unit test, integration test.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _service.DeleteManyAsync(ids);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (GenericServiceException e)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(new BasicResponse
|
|
|
|
|
{
|
|
|
|
|
Message = e.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|