-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublisherController.cs
115 lines (108 loc) · 3.58 KB
/
PublisherController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using csharp_webapi_example.ActionResults;
using csharp_webapi_example.Data.Models;
using csharp_webapi_example.Exceptions;
using csharp_webapi_example.Services;
using csharp_webapi_example.ViewModels;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace csharp_webapi_example.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PublisherController : ControllerBase
{
private readonly PublisherService _publisherService;
private readonly ILogger<PublisherController> _logger;
public PublisherController(PublisherService publisherService, ILogger<PublisherController> logger)
{
_publisherService = publisherService;
_logger = logger;
}
[HttpGet]
public IActionResult GetAllPublishers()
{
try
{
_logger.LogInformation("Loggin from Publihser GetAllPublishers()");
var _result = _publisherService.GetAllPublishers();
return Ok(_result);
}
catch (Exception)
{
return BadRequest("Error loading publishers");
}
}
public IActionResult GetAllPublishers(string sortBy, string searchString, int pageNumber)
{
try
{
_logger.LogInformation("Loggin Publihser GetAllPublishers()");
var _result = _publisherService.GetAllPublishers(sortBy, searchString, pageNumber);
return Ok(_result);
}
catch (Exception)
{
return BadRequest("Error loading publishers");
}
}
[HttpPost]
public IActionResult AddPublisher([FromBody] PublisherVM publisher)
{
try
{
var oPublisher = _publisherService.AddPublisher(publisher);
return Created(nameof(AddPublisher), oPublisher);
}
catch (PublisherNameException ex)
{
return BadRequest($"{ex.Message}, Publisher name: {ex.PublisherName}");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("{id}")]
public IActionResult GetPublisherById(int id)
{
var _response = _publisherService.GetPublisherById(id);
if (_response != null)
{
return Ok(_response);
//var _responseObj = new CustomActionResultVM()
//{
// Publisher = _response,
//};
//return new CustomActionResult(_responseObj);
}
else
{
return NotFound();
//var _responseObj = new CustomActionResultVM()
//{
// Exception = new Exception($"This is coming from {nameof(GetPublisherById)} in {this.GetType().Name}"),
//};
//return new CustomActionResult(_responseObj);
}
}
[HttpGet("{id}/full")]
public IActionResult GetPublisherData(int id)
{
var _response = _publisherService.GetPublisherData(id);
return Ok(_response);
}
[HttpDelete]
public IActionResult DeletePublisher(int id)
{
try
{
_publisherService.Delete(id);
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}