Skip to content

Instantly share code, notes, and snippets.

@profesor79
Last active March 11, 2023 19:37
Show Gist options
  • Save profesor79/e782b93d13a508d34e895b125f1d3a27 to your computer and use it in GitHub Desktop.
Save profesor79/e782b93d13a508d34e895b125f1d3a27 to your computer and use it in GitHub Desktop.
When you know what you do - then the code is better
[HttpPost]
public HttpResponseMessage UpdateElementIdManual(Parameters parameters)
{
_logger.Debug("some useful info here");
try
{
var eletran = ctx.ElementTransactions.Where(s => s.ElementNo == parameters.ElementId).First();
var dt = DateTime.Parse(eletran.DocumentPeriod + "-01");
var documentPeriod = string.IsNullOrWhiteSpace(eletran.DocumentPeriodType) ? $"{dt:MMM}-{dt:yy}" : $"{dt:MMM}-{dt:yy} {eletran.DocumentPeriodType}";
var inv = ctx.Elements.Where(s => s.ElementNo == parameters.ElementId).ToList();
var minTableName = ctx.TableName.Where(s => s.ElementId == parameters.ElementId).First();
// helper variable to decipher logic
var invCountIsZero = inv.Count == 0;
var sameEntity = minTableName.Entity == eletran.Customer;
var sameMonth = minTableName.Month == documentPeriod;
if (invCountIsZero == 0 && sameEntity && sameMonth)
{
eletran.TranStatus = "Manually Generated";
eletran.ElementNo = parameters.ElementId;
ctx.SaveChanges();
// what we shall return here is....just response
// that means when there is ok and text, then we have an issue
// so returning ok + text means an error and the schema is broken ... wtf
return new HttpResponseMessage();
}
return new HttpResponseMessage
{
Content = new StringContent($"REDACTED1 Same month:{sameMonth}, Same entity: {sameEntity}"),
StatusCode = System.Net.HttpStatusCode.BadRequest,
};
// we shall catch null
catch (InvalidOperationException noData)
{
_logger.Error(noData);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.NotFound,
};
}
catch (Exception ex)
{
_logger.Error(ex);
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment