I'm implementing a system, which requires an order to be active. I have a class called Yritys, which is the customer account who would have credits.
1 credit would mean 1 day of service use.
Yritys (company) has properties for credit (amount of credit) and OrderStartDate (when the original order was placed, credits were bought)
in the mongoDbRepository we Set Order like this:
public async Task<DateTime> SetOrder(int companyId, int amount)
{
var filter = Builders<Yritys>.Filter.Eq(_C => _C.yritysId, companyId);
var company = await _companyCollection.Find(filter).FirstAsync();
company.OrderStartDate = DateTime.Today;
company.credit = amount;
await _companyCollection.ReplaceOneAsync(filter, company);
return company.OrderStartDate;
}
Order Update method:
public async Task<DateTime> UpdateOrder(int companyId, int amount)
{
var filter = Builders<Yritys>.Filter.Eq(_x => _x.yritysId, companyId);
var company = await _companyCollection.Find(filter).FirstAsync();
if (GetRemainingOrder(companyId) > 0)
{
company.credit += amount;
}
await _companyCollection.ReplaceOneAsync(filter, company);
return company.OrderStartDate;
}
Check for Remaining order:
public int GetRemainingOrder(int companyId)
{
var filter = Builders<Yritys>.Filter.Eq(_c => _c.yritysId, companyId);
var company = _companyCollection.Find(filter).First();
var expiryDate = company.OrderStartDate.AddDays(company.credit);
if (expiryDate == DateTime.MinValue) { return 0; }
if (DateTime.Compare(expiryDate, DateTime.UtcNow) == 1)
{
//order is active
return (int)MathF.Floor((float)(expiryDate - DateTime.UtcNow).TotalDays);
}
// order not active: credits = 0
return 0;
}
And lastly we print the date when the order expires
public string GetDueDate(int companyId)
{
var filter = Builders<Yritys>.Filter.Eq(company => company.yritysId, companyId);
var company = _companyCollection.Find(filter).First();
int credits = company.credit;
if (credits > 0)
{
DateTime dueDateInTheFuture = company.OrderStartDate.AddDays(credits);
return $"{dueDateInTheFuture.Day}.{dueDateInTheFuture.Month}.{dueDateInTheFuture.Year}";
}
else return "Tee uusi tilaus";
}
Is there a better way to implement a credit-based order system in asp.net?
which requires an order to be active
active for what ? please give us more details on this part of your business requirement. and what does the credits used for in the system ? \$\endgroup\$public async Task<DateTime> SetOrder(int companyId, int amount)
isamount
the number of credits purchased? \$\endgroup\$