-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from AdityaP700/feature/mongodb-integration
Feature/mongodb integration branch to be merged
- Loading branch information
Showing
24 changed files
with
1,273 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using SentimatrixAPI.Models; | ||
using SentimatrixAPI.Services; | ||
|
||
namespace SentimatrixAPI.Controllers | ||
{ | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class EmailController : ControllerBase | ||
{ | ||
private readonly EmailService _emailService; | ||
private readonly ILogger<EmailController> _logger; | ||
private readonly IMongoCollection<EmailData> _emailCollection; | ||
|
||
public EmailController( | ||
EmailService emailService, | ||
ILogger<EmailController> logger, | ||
IMongoDatabase database) | ||
{ | ||
_emailService = emailService ?? throw new ArgumentNullException(nameof(emailService)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_emailCollection = database.GetCollection<EmailData>("email"); | ||
// Log a message indicating successful connection | ||
_logger.LogInformation("Successfully connected to the Email database."); | ||
} | ||
|
||
[HttpGet("sentiment/{period}")] | ||
public async Task<IActionResult> GetSentimentTrend(string period) | ||
{ | ||
try | ||
{ | ||
DateTime startDate = period.ToUpper() switch | ||
{ | ||
"1D" => DateTime.UtcNow.AddDays(-1), | ||
"5D" => DateTime.UtcNow.AddDays(-5), | ||
"1W" => DateTime.UtcNow.AddDays(-7), | ||
"1M" => DateTime.UtcNow.AddMonths(-1), | ||
_ => throw new ArgumentException("Invalid time period") | ||
}; | ||
|
||
var results = await _emailCollection | ||
.Aggregate() | ||
.Match(Builders<EmailData>.Filter.Gte(e => e.ReceivedDate, startDate)) | ||
.Group(e => e.ReceivedDate.Date, | ||
g => new SentimentData | ||
{ | ||
Period = g.Key.ToString("yyyy-MM-dd"), | ||
AverageScore = g.Average(x => x.Score), | ||
Count = g.Count() | ||
}) | ||
.Sort(Builders<SentimentData>.Sort.Ascending(x => x.Period)) | ||
.ToListAsync(); | ||
|
||
return Ok(results); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving sentiment trend"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<EmailData>>> GetAllEmails() | ||
{ | ||
try | ||
{ | ||
var emails = await _emailCollection.Find(new BsonDocument()).ToListAsync(); | ||
return Ok(emails); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving all emails"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpGet("positive")] | ||
public async Task<ActionResult<IEnumerable<EmailData>>> GetPositiveEmails( | ||
[FromQuery] int page = 1, | ||
[FromQuery] int pageSize = 10) | ||
{ | ||
try | ||
{ | ||
var emails = await _emailService.GetEmailsByScoreRangeAsync(70, 100); | ||
var paginatedEmails = emails | ||
.Skip((page - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToList(); | ||
|
||
return Ok(new | ||
{ | ||
Data = paginatedEmails, | ||
Page = page, | ||
PageSize = pageSize, | ||
TotalCount = emails.Count() | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving positive emails"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpGet("negative")] | ||
public async Task<ActionResult<IEnumerable<EmailData>>> GetNegativeEmails( | ||
[FromQuery] int page = 1, | ||
[FromQuery] int pageSize = 10) | ||
{ | ||
try | ||
{ | ||
var emails = await _emailService.GetEmailsByScoreRangeAsync(0, 30); | ||
var paginatedEmails = emails | ||
.Skip((page - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToList(); | ||
|
||
return Ok(new | ||
{ | ||
Data = paginatedEmails, | ||
Page = page, | ||
PageSize = pageSize, | ||
TotalCount = emails.Count() | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving negative emails"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpGet("sender/{email}")] | ||
public async Task<ActionResult<IEnumerable<EmailData>>> GetEmailsBySender( | ||
string email, | ||
[FromQuery] int page = 1, | ||
[FromQuery] int pageSize = 10) | ||
{ | ||
try | ||
{ | ||
var emails = await _emailService.GetEmailsBySenderAsync(email); | ||
var paginatedEmails = emails | ||
.Skip((page - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToList(); | ||
|
||
return Ok(new | ||
{ | ||
Data = paginatedEmails, | ||
Page = page, | ||
PageSize = pageSize, | ||
TotalCount = emails.Count() | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving emails by sender"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpGet("stats")] | ||
public async Task<ActionResult<DashboardStats>> GetDashboardStats() | ||
{ | ||
try | ||
{ | ||
var stats = await _emailService.GetDashboardStatsAsync(); | ||
return Ok(stats); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error retrieving dashboard stats"); | ||
return StatusCode(500, new { message = "Internal server error", details = ex.Message }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// using Microsoft.AspNetCore.Mvc; | ||
|
||
// using MongoDB.Driver; | ||
|
||
// public class YourController : ControllerBase | ||
// { | ||
// private readonly MongoDbContext _mongoDbContext; | ||
|
||
// public YourController(MongoDbContext mongoDbContext) | ||
// { | ||
// _mongoDbContext = mongoDbContext; | ||
// } | ||
|
||
// [HttpGet] | ||
// public async Task<IActionResult> GetItems() | ||
// { | ||
// var items = await _mongoDbContext.GetCollection<Item>("items").Find(_ => true).ToListAsync(); | ||
// return Ok(items); | ||
// } | ||
|
||
// [HttpPost] | ||
// public async Task<IActionResult> CreateItem(Item item) | ||
// { | ||
// await _mongoDbContext.GetCollection<Item>("items").InsertOneAsync(item); | ||
// return CreatedAtAction(nameof(GetItems), item); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace SentimatrixAPI.Data | ||
{ | ||
public class MongoDBSettings | ||
{ | ||
public string ConnectionString { get; set; } = string.Empty; | ||
public string DatabaseName { get; set; } = string.Empty; | ||
public string EmailsCollectionName { get; set; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using MongoDB.Driver; | ||
using MongoDB.Bson; | ||
|
||
namespace MyApp | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
const string connectionUri = "mongodb+srv://user001:[email protected]/?retryWrites=true&w=majority&appName=Cluster0"; | ||
var settings = MongoClientSettings.FromConnectionString(connectionUri); | ||
|
||
// Set the ServerApi field of the settings object to set the version of the Stable API on the client | ||
settings.ServerApi = new ServerApi(ServerApiVersion.V1); | ||
|
||
// Create a new client and connect to the server | ||
var client = new MongoClient(settings); | ||
|
||
// Send a ping to confirm a successful connection | ||
try | ||
{ | ||
var result = client.GetDatabase("sentimatrix").RunCommand<BsonDocument>(new BsonDocument("ping", 1)); | ||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace SentimatrixAPI.Models | ||
{ | ||
public class DashboardStats | ||
{ | ||
public int TotalEmails { get; set; } | ||
public int PositiveEmails { get; set; } | ||
public int NegativeEmails { get; set; } | ||
public double AverageSentimentScore { get; set; } | ||
} | ||
} |
Oops, something went wrong.