yavsc/src/Api/Controllers/Survey/BugApiController.cs

183 lines
5 KiB
C#
Raw Normal View History

2019-08-26 15:06:57 +01:00
using Newtonsoft.Json;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
2019-05-18 09:42:50 +01:00
using Yavsc.Models;
using Yavsc.Models.IT.Fixing;
2023-03-19 17:57:55 +00:00
using Microsoft.EntityFrameworkCore;
2019-05-18 09:42:50 +01:00
namespace Yavsc.ApiControllers
{
[Produces("application/json")]
2019-08-26 01:36:23 +01:00
[Route("~/api/bug")]
2019-05-18 09:42:50 +01:00
public class BugApiController : Controller
{
2020-09-12 01:11:30 +01:00
private readonly ApplicationDbContext _context;
readonly ILogger _logger;
2019-05-18 09:42:50 +01:00
public BugApiController(ApplicationDbContext context, ILoggerFactory factory)
{
_logger = factory.CreateLogger<BugApiController>();
_context = context;
}
2019-08-26 15:06:57 +01:00
[HttpPost("signal")]
[Authorize]
public IActionResult Signal([FromBody] BugReport report)
2019-05-18 09:42:50 +01:00
{
2019-08-26 15:06:57 +01:00
_logger.LogWarning("bug reported : "+report.ExceptionObjectJson);
if (report.ExceptionObjectJson!=null)
if (report.ExceptionObjectJson.Length > 10240)
report.ExceptionObjectJson = report.ExceptionObjectJson.Substring(0,10240);
2019-05-18 09:42:50 +01:00
if (!ModelState.IsValid)
2019-08-26 15:06:57 +01:00
{
_logger.LogError("Bag request :");
_logger.LogError(JsonConvert.SerializeObject(ModelState));
return new BadRequestObjectResult(ModelState);
}
var existent = _context.Bug.FirstOrDefault( b =>
b.Description == report.ExceptionObjectJson &&
b.Title == report.Component &&
b.Status != BugStatus.Rejected
2019-05-18 09:42:50 +01:00
);
if (existent != null)
2019-08-26 15:06:57 +01:00
{
_logger.LogInformation("bug already reported");
2019-05-18 09:42:50 +01:00
return Ok(existent);
2019-08-26 15:06:57 +01:00
}
_logger.LogInformation("Filling a new bug report");
var bug = new Bug {
Title = report.Component,
Description = report.ExceptionObjectJson
};
_context.Bug.Add(bug);
_context.SaveChanges();
return CreatedAtRoute("GetBug", new { id = bug.Id }, bug);
2019-05-18 09:42:50 +01:00
}
// GET: api/BugApi
[HttpGet]
public IEnumerable<Bug> GetBug()
{
return _context.Bug;
}
// GET: api/bug/5
[HttpGet("{id}", Name = "GetBug")]
public async Task<IActionResult> GetBug([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-18 09:42:50 +01:00
}
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
if (bug == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-18 09:42:50 +01:00
}
return Ok(bug);
}
// PUT: api/BugApi/5
[HttpPut("{id}")]
public async Task<IActionResult> PutBug([FromRoute] long id, [FromBody] Bug bug)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-18 09:42:50 +01:00
}
if (id != bug.Id)
{
2023-03-19 17:57:55 +00:00
return BadRequest();
2019-05-18 09:42:50 +01:00
}
_context.Entry(bug).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!BugExists(id))
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-18 09:42:50 +01:00
}
else
{
throw;
}
}
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status204NoContent);
2019-05-18 09:42:50 +01:00
}
2019-08-21 20:05:07 +01:00
// POST: api/bug
2019-05-18 09:42:50 +01:00
[HttpPost]
public async Task<IActionResult> PostBug([FromBody] Bug bug)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-18 09:42:50 +01:00
}
_context.Bug.Add(bug);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (BugExists(bug.Id))
{
2023-03-19 17:57:55 +00:00
return new StatusCodeResult(StatusCodes.Status409Conflict);
2019-05-18 09:42:50 +01:00
}
else
{
throw;
}
}
return CreatedAtRoute("GetBug", new { id = bug.Id }, bug);
}
// DELETE: api/BugApi/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteBug([FromRoute] long id)
{
if (!ModelState.IsValid)
{
2023-03-19 17:57:55 +00:00
return BadRequest(ModelState);
2019-05-18 09:42:50 +01:00
}
Bug bug = await _context.Bug.SingleAsync(m => m.Id == id);
if (bug == null)
{
2023-03-19 17:57:55 +00:00
return NotFound();
2019-05-18 09:42:50 +01:00
}
_context.Bug.Remove(bug);
await _context.SaveChangesAsync();
return Ok(bug);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_context.Dispose();
}
base.Dispose(disposing);
}
private bool BugExists(long id)
{
return _context.Bug.Count(e => e.Id == id) > 0;
}
}
2019-08-21 20:05:07 +01:00
}