-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller - PartiesApiController.cs
182 lines (158 loc) · 4.16 KB
/
Controller - PartiesApiController.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Snippet.Models;
using Snippet.Models.Domain.Parties;
using Snippet.Models.Requests.Parties;
using Snippet.Services.Interfaces.Parties;
using Snippet.Web.Controllers;
using Snippet.Web.Models.Responses;
using System;
namespace Snippet.Web.Api.Controllers.Parties
{
[Route("api/parties")]
[ApiController]
public class PartiesApiController : BaseApiController
{
private IPartiesService _service = null;
public PartiesApiController(IPartiesService service, ILogger<PartiesApiController> logger) : base(logger)
{
_service = service;
}
[HttpGet("search/all")]
public ActionResult<ItemsResponse<Paged<Party>>> GetSearchPaginatedALL(int pageIndex, int pageSize, string query)
{
ActionResult result = null;
try
{
Paged<Party> paged = _service.GetSearchPaginatedALL(pageIndex, pageSize, query);
if(paged == null)
{
result = NotFound404(new ErrorResponse("Record not found"));
}
else
{
ItemResponse<Paged<Party>> response = new ItemResponse<Paged<Party>>();
response.Item = paged;
result = Ok200(response);
}
}
catch(Exception ex)
{
Logger.LogError(ex.ToString());
result = StatusCode(500, new ErrorResponse(ex.Message.ToString()));
}
return result;
}
[HttpGet("search/coalition")]
public ActionResult<ItemsResponse<Paged<Party>>> GetSearchPaginatedCoalition(int pageIndex, int pageSize, string query)
{
ActionResult result = null;
try
{
Paged<Party> paged = _service.GetSearchPaginatedCoalition(pageIndex, pageSize, query);
if(paged == null)
{
result = NotFound404(new ErrorResponse("Record not found"));
}
else
{
ItemResponse<Paged<Party>> response = new ItemResponse<Paged<Party>>();
response.Item = paged;
result = Ok200(response);
}
}
catch(Exception ex)
{
Logger.LogError(ex.ToString());
result = StatusCode(500, new ErrorResponse(ex.Message.ToString()));
}
return result;
}
[HttpGet("search/noncoalition")]
public ActionResult<ItemsResponse<Paged<Party>>> GetSearchPaginatedNonCoalition(int pageIndex, int pageSize, string query)
{
ActionResult result = null;
try
{
Paged<Party> paged = _service.GetSearchPaginatedNonCoalition(pageIndex, pageSize, query);
if(paged == null)
{
result = NotFound404(new ErrorResponse("Record not found"));
}
else
{
ItemResponse<Paged<Party>> response = new ItemResponse<Paged<Party>>();
response.Item = paged;
result = Ok200(response);
}
}
catch(Exception ex)
{
Logger.LogError(ex.ToString());
result = StatusCode(500, new ErrorResponse(ex.Message.ToString()));
}
return result;
}
[HttpGet("{id:int}")]
public ActionResult<ItemsResponse<Party>> GetByIdAll(int id)
{
int iCode = 200;
BaseResponse response = null;
try
{
Party party = _service.GetByIdAll(id);
if(party == null)
{
iCode = 404;
response = new ErrorResponse("Record not found");
}
else
{
response = new ItemResponse<Party> { Item = party };
}
}
catch(Exception ex)
{
iCode = 500;
base.Logger.LogError(ex.ToString());
response = new ErrorResponse($"ArgumentException Error 500: {ex.Message}");
}
return StatusCode(iCode, response);
}
[HttpPost]
public ActionResult<ItemResponse<int>> AddParty(PartyAddRequest model)
{
ObjectResult result = null;
try
{
int id = _service.AddParty(model);
ItemResponse<int> response = new ItemResponse<int>() { Item = id };
result = Created201(response);
}
catch(Exception ex)
{
Logger.LogError(ex.ToString());
ErrorResponse response = new ErrorResponse(ex.Message);
result = StatusCode(500, response);
}
return result;
}
[HttpPut("{id:int}")]
public ActionResult<SuccessResponse> UpdateParty(PartyUpdateRequest model, int Id)
{
int code = 200;
BaseResponse response = null;
try
{
_service.UpdateParty(model, Id);
response = new SuccessResponse();
}
catch(Exception ex)
{
code = 500;
response = new ErrorResponse(ex.Message);
}
return StatusCode(code, response);
}
}
}