I would like to know how to know how can I optimize my current Post method , I initialize object where I am creating list of json items. Also how can I reduce many if and if else statments? For this post and get methods I am using swagger , is it possible to declare exceptions objects somewhere globaly? What kind of good practice there is regarding swagger objects, maybe I have to declare somewhere as json structure?
[HttpPost]
public IHttpActionResult Post([FromBody] SpecCaseRequest specCaseRequest)
{
var specCase= new SpecCase();
specCase.CopyFromRequest(specCaseRequest);
ExceptionResponse[] data = new ExceptionResponse[]
{
new ExceptionResponse { Id = 1, Message = "some text", ReturnCode = HttpStatusCode.BadRequest },
new ExceptionResponse { Id = 2, Message = "some text", ReturnCode = HttpStatusCode.BadRequest },
new ExceptionResponse { Id = 3, Message = "some text", ReturnCode = HttpStatusCode.Created },
new ExceptionResponse { Id = 4, Message = "some text", ReturnCode = HttpStatusCode.BadRequest },
new ExceptionResponse { Id = 5, Message = "some text", ReturnCode = HttpStatusCode.BadRequest },
new ExceptionResponse { Id = 7, Message = "some text", ReturnCode = HttpStatusCode.BadRequest },
};
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
JArray jo = JArray.Parse(json);
HttpResponseMessage responseMsg2 = new HttpResponseMessage(HttpStatusCode.BadRequest);
if (specCase.Name == "")
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 2)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCase.Name == null)
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 1)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCase.Signatories.Select(x => x.SignatoryId == "" && x.Type == SignatoryType.Company).FirstOrDefault())
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 4)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCaseRequest.Signatories.Select(x => x.Type != SignatoryType.Company).FirstOrDefault())
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 8)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCaseRequest.Signatories.Select(x => x.Id == "").FirstOrDefault())
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 9)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCase.Signatories.Select(x => x.SignatoryId == null).FirstOrDefault())
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 5)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
else if (specCase.Signatories.Select(x => x.SignatoryId == "").FirstOrDefault())
{
JObject match = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 7)
.FirstOrDefault();
responseMsg2.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg2.Content = new StringContent(match.ToString(), Encoding.UTF8, "application/json");
responseMsg2.Content.ReadAsStringAsync();
IHttpActionResult response2 = ResponseMessage(responseMsg2);
return response2;
}
try
{
var specCaseRequest= unitOfWork.GetSigningCaseRepository();
specCaseRequest.Add(specCase);
unitOfWork.Save();
}
catch (Exception e)
{
Log.Error(e.Message);
return InternalServerError();
}
JObject match1 = jo.Values<JObject>()
.Where(m => m["Id"].Value<int>() == 3)
.FirstOrDefault();
HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.Created);
responseMsg.Headers.Location = new Uri(Url.Link("DefaultApi", new { controller = "SpecCases", id = specCase.Id }));
responseMsg.Content = new StringContent(match1.ToString(), Encoding.UTF8, "application/json");
responseMsg.Content.ReadAsStringAsync();
IHttpActionResult response = ResponseMessage(responseMsg);
return response;
}