-
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 #16 from Purdue-ACM-SIGAPP/Soleil
Soleil
- Loading branch information
Showing
2 changed files
with
163 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using SimpleWebAppReact.Entities; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Driver; | ||
using SimpleWebAppReact.Services; | ||
|
||
namespace SimpleWebAppReact.Controllers | ||
{ | ||
/// <summary> | ||
/// Defines endpoints for operations relating the Events table | ||
/// </summary> | ||
[ApiController] | ||
[Route("api/events")] | ||
public class EventsController : ControllerBase | ||
{ | ||
private readonly ILogger<EventsController> _logger; | ||
private readonly IMongoCollection<Events>? _events; | ||
|
||
public EventsController(ILogger<EventsController> logger, MongoDbService mongoDbService) | ||
{ | ||
_logger = logger; | ||
_events = mongoDbService.Database?.GetCollection<Events>("events"); | ||
} | ||
|
||
/// <summary> | ||
/// gets events, with optional query parameters | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
public async Task<IEnumerable<Events>> Get([FromQuery] string? eventName = null, [FromQuery] string? summary = null, [FromQuery] string? content = null, [FromQuery] string? userID = null, [FromQuery] DateTime? date = null, [FromQuery] string? address = null) | ||
{ | ||
// Build the filter using a filter builder | ||
var filterBuilder = Builders<Events>.Filter; | ||
var filter = FilterDefinition<Events>.Empty; | ||
|
||
// Apply the event name filter if the parameter is provided | ||
if (!string.IsNullOrEmpty(eventName)) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.EventName, eventName); | ||
} | ||
|
||
// Apply the summary filter if the parameter is provided | ||
if (!string.IsNullOrEmpty(summary)) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.Summary, summary); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(content)) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.Content, content); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(userID)) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.UserID, userID); | ||
} | ||
|
||
if (!date.HasValue) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.Date, date); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(address)) | ||
{ | ||
filter &= filterBuilder.Eq(b => b.Address, address); | ||
} | ||
|
||
// Fetch the events from the database using the filter | ||
return await _events.Find(filter).ToListAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// gets specific events with id | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpGet("{id}")] | ||
|
||
//deleted asyn | ||
public async Task<ActionResult<Events?>> GetById(string id) | ||
Check warning on line 80 in Controllers/EventsController.cs GitHub Actions / test
Check warning on line 80 in Controllers/EventsController.cs GitHub Actions / test
|
||
{ | ||
// Simple validation to check if the ID is not null | ||
if (string.IsNullOrEmpty(id)) | ||
{ | ||
return BadRequest("Invalid ID format."); | ||
} | ||
|
||
var filter = Builders<Events>.Filter.Eq(x => x.Id, id); | ||
var events = _events.Find(filter).FirstOrDefault(); | ||
return events is not null ? Ok(events) : NotFound(); | ||
} | ||
|
||
/// <summary> | ||
/// adds events entry to table | ||
/// </summary> | ||
/// <param name="events"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public async Task<ActionResult> Post(Events events) | ||
{ | ||
await _events.InsertOneAsync(events); | ||
return CreatedAtAction(nameof(GetById), new { id = events.Id }, events); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// updates a events entry | ||
/// </summary> | ||
/// <param name="events"></param> | ||
/// <returns></returns> | ||
[HttpPut] | ||
public async Task<ActionResult> Update(Events events) | ||
{ | ||
var filter = Builders<Events>.Filter.Eq(x => x.Id, events.Id); | ||
await _events.ReplaceOneAsync(filter, events); | ||
return Ok(); | ||
} | ||
|
||
/// <summary> | ||
/// deletes a events entry | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpDelete("{id}")] | ||
public async Task<ActionResult> Delete(string id) | ||
{ | ||
var filter = Builders<Events>.Filter.Eq(x => x.Id, id); | ||
await _events.DeleteOneAsync(filter); | ||
return Ok(); | ||
} | ||
} | ||
} | ||
|
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,30 @@ | ||
namespace SimpleWebAppReact.Entities; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
/// <summary> | ||
/// Class structure matches 1-1 with Building Table in database | ||
/// </summary> | ||
public class Events | ||
{ | ||
[BsonId] | ||
[BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] | ||
public string? Id { get; set; } | ||
|
||
[BsonElement("eventName"), BsonRepresentation(BsonType.String)] | ||
public string? EventName { get; set; } | ||
|
||
[BsonElement("summary"), BsonRepresentation(BsonType.String)] | ||
public string? Summary { get; set; } | ||
|
||
[BsonElement("content"), BsonRepresentation(BsonType.String)] | ||
public string? Content { get; set; } | ||
|
||
[BsonElement("userID"), BsonRepresentation(BsonType.String)] | ||
public string? UserID { get; set; } | ||
|
||
[BsonElement("date"), BsonRepresentation(BsonType.DateTime)] | ||
public DateTime? Date { get; set; } | ||
|
||
[BsonElement("address"), BsonRepresentation(BsonType.String)] | ||
public string? Address { get; set; } | ||
} |