I’m writing an API for my project and one of its functions is to sort data received from the database. Using everything locally on my laptop and Swagger, everything works as it should, but when I run the same code on the server, I start having problems with the date. Sorting breaks and returns empty lists
I tried to fix it with different methods, such as formatting the received data, but it did not solve the problem
My code:
public class Sort
{
private readonly IMongoDatabase _database;
public double sumExpense { get; private set; } = 0.0;
public Sort()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
string? mongodbKey = configuration["Tokens:MongoDbConnection"];
MongoClient client = new(mongodbKey);
_database = client.GetDatabase("finances");
}
public async Task<BsonDocument> RunAsync(AuthData request, double ID)
{
var collection = _database.GetCollection<BsonDocument>("Expense");
string format = "yyyy-MM-dd";
if (!DateTime.TryParseExact(request.FirstDate, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime firstDateTime) ||
!DateTime.TryParseExact(request.LastDate, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime lastDateTime))
{
return new BsonDocument
{
{ "Error", "Invalid date format" }
};
}
var startOfFirstDay = new DateTime(firstDateTime.Year, firstDateTime.Month, firstDateTime.Day, 0, 0, 0, DateTimeKind.Unspecified);
var endOfLastDay = new DateTime(lastDateTime.Year, lastDateTime.Month, lastDateTime.Day, 23, 59, 59, DateTimeKind.Unspecified);
var idFilter = Builders<BsonDocument>.Filter.Eq("User", ID);
var dateFilter = Builders<BsonDocument>.Filter.Gte("Date", startOfFirstDay) &
Builders<BsonDocument>.Filter.Lte("Date", endOfLastDay);
var combinedFilter = idFilter & dateFilter;
var sortedByCategoryTask = AsyncSortingByCategory(collection, combinedFilter);
var sortedByTagsTask = AsyncSortingByTegs(collection, combinedFilter);
await Task.WhenAll(sortedByCategoryTask, sortedByTagsTask);
var bsonDocument = new BsonDocument
{
{ "Category", sortedByCategoryTask.Result },
{ "Tegs", sortedByTagsTask.Result }
};
return bsonDocument;
}
}