| | | 1 | | using System; |
| | | 2 | | using System.Net; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Newtonsoft.Json; |
| | | 6 | | using SVETA.Api.Data.DTO; |
| | | 7 | | |
| | | 8 | | namespace SVETA.Api.Controllers |
| | | 9 | | { |
| | | 10 | | public class SvetaController : ControllerBase |
| | | 11 | | { |
| | | 12 | | public const string STR_SAVED_OBJECT = "SavedObject"; |
| | | 13 | | |
| | | 14 | | private ILogger<SvetaController> _logger; |
| | | 15 | | |
| | 0 | 16 | | public SvetaController(ILogger<SvetaController> logger) |
| | 0 | 17 | | { |
| | 0 | 18 | | _logger = logger; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | protected ActionResult ForbidResult(Exception ex) |
| | 0 | 22 | | { |
| | 0 | 23 | | return StatusCode((int)HttpStatusCode.Forbidden, new ErrorDTO(ex)); |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | protected ActionResult ForbidResult(string message) |
| | 0 | 27 | | { |
| | 0 | 28 | | return StatusCode((int)HttpStatusCode.Forbidden, new ErrorDTO(message)); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | protected ActionResult ForbidResult() |
| | 0 | 32 | | { |
| | 0 | 33 | | return ForbidResult("Нет прав"); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected NotFoundObjectResult NotFoundResult(Exception ex) |
| | 0 | 37 | | { |
| | 0 | 38 | | return NotFound(new ErrorDTO(ex)); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | protected NotFoundObjectResult NotFoundResult(string message) |
| | 0 | 42 | | { |
| | 0 | 43 | | return NotFound(new ErrorDTO(message)); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | protected NotFoundObjectResult NotFoundResult() |
| | 0 | 47 | | { |
| | 0 | 48 | | return NotFoundResult("Запись не найдена"); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | protected BadRequestObjectResult BadRequestResult(Exception ex) |
| | 0 | 52 | | { |
| | 0 | 53 | | return BadRequest(new ErrorDTO(ex)); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | protected BadRequestObjectResult BadRequestResult(ErrorDTO error) |
| | 0 | 57 | | { |
| | 0 | 58 | | return BadRequest(error); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | protected BadRequestObjectResult BadRequestResult(string message) |
| | 0 | 62 | | { |
| | 0 | 63 | | return BadRequest(new ErrorDTO(message)); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | protected ActionResult ServerError(Exception ex) |
| | 0 | 67 | | { |
| | | 68 | | try |
| | 0 | 69 | | { |
| | 0 | 70 | | if (ex.Data[STR_SAVED_OBJECT] != null) |
| | 0 | 71 | | { |
| | 0 | 72 | | var json = JsonConvert.SerializeObject(ex.Data[STR_SAVED_OBJECT]); |
| | 0 | 73 | | _logger.LogError("Saved object: " + json); |
| | 0 | 74 | | } |
| | 0 | 75 | | _logger.LogError(message: ex.Message, exception: ex); |
| | 0 | 76 | | } |
| | 0 | 77 | | catch { } |
| | | 78 | | |
| | 0 | 79 | | return StatusCode(500, new ErrorDTO(ex)); |
| | 0 | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |