-
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 #24 from Purdue-ACM-SIGAPP/add-video-tour
Add video tour
- Loading branch information
Showing
2 changed files
with
127 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,94 @@ | ||
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 VideoTour table | ||
/// </summary> | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class VideoTourController : ControllerBase | ||
{ | ||
private readonly ILogger<VideoTourController> _logger; | ||
private readonly IMongoCollection<VideoTour>? _videoTours; | ||
|
||
public VideoTourController(ILogger<VideoTourController> logger, MongoDbService mongoDbService) | ||
{ | ||
_logger = logger; | ||
_videoTours = mongoDbService.Database?.GetCollection<VideoTour>("videoTour"); | ||
} | ||
|
||
/// <summary> | ||
/// gets videoTours | ||
/// </summary> | ||
[HttpGet] | ||
public async Task<ActionResult<VideoTour>> Get() | ||
{ | ||
// Fetch the videoTours from the database | ||
var videoTours = await _videoTours.Find(_ => true).ToListAsync(); | ||
|
||
return Ok(videoTours); | ||
} | ||
|
||
/// <summary> | ||
/// gets specific videoTour with id | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<VideoTour?>> GetById(string id) | ||
Check warning on line 43 in Controllers/VideoTourController.cs GitHub Actions / test
|
||
{ | ||
if (!ModelState.IsValid) { | ||
return BadRequest(ModelState); | ||
} | ||
|
||
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, id); | ||
var videoTour = _videoTours.Find(filter).FirstOrDefault(); | ||
|
||
return videoTour is not null ? Ok(videoTour) : NotFound(); | ||
} | ||
|
||
/// <summary> | ||
/// adds videoTour entry to table | ||
/// </summary> | ||
/// <param name="videoTour"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public async Task<ActionResult> Post(VideoTour videoTour) | ||
{ | ||
await _videoTours.InsertOneAsync(videoTour); | ||
return CreatedAtAction(nameof(GetById), new { id = videoTour.Id }, videoTour); | ||
} | ||
|
||
/// <summary> | ||
/// updates a videoTour entry | ||
/// </summary> | ||
/// <param name="videoTour"></param> | ||
/// <returns></returns> | ||
[HttpPut] | ||
public async Task<ActionResult> Update(VideoTour videoTour) | ||
{ | ||
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, videoTour.Id); | ||
await _videoTours.ReplaceOneAsync(filter, videoTour); | ||
return Ok(); | ||
} | ||
|
||
/// <summary> | ||
/// deletes a videoTour entry | ||
/// </summary> | ||
/// <param name="id"></param> | ||
/// <returns></returns> | ||
[HttpDelete("{id}")] | ||
public async Task<ActionResult> Delete(string id) | ||
{ | ||
var filter = Builders<VideoTour>.Filter.Eq(x => x.Id, id); | ||
await _videoTours.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,33 @@ | ||
namespace SimpleWebAppReact.Entities; | ||
using MongoDB.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class VideoTour | ||
{ | ||
[BsonId] | ||
[BsonElement("_id"), BsonRepresentation(BsonType.ObjectId)] | ||
public string? Id { get; set; } | ||
|
||
[BsonElement("title"), BsonRepresentation(BsonType.String)] | ||
public string? Title { get; set; } | ||
|
||
[BsonElement("url"), BsonRepresentation(BsonType.String)] | ||
public string? Url { get; set; } | ||
|
||
[BsonElement("length"), BsonRepresentation(BsonType.Double)] | ||
public Double? Length { get; set; } | ||
|
||
[BsonElement("createdAt"), BsonRepresentation(BsonType.DateTime)] | ||
public DateTime CreatedAt { get; set; } = DateTime.Now; | ||
|
||
[BsonElement("description"), BsonRepresentation(BsonType.String)] | ||
public string? Description { get; set; } | ||
|
||
[BsonElement("userId"), BsonRepresentation(BsonType.String)] | ||
public string? UserId { get; set; } | ||
|
||
[BsonElement("buildingId"), BsonRepresentation(BsonType.String)] | ||
public string? BuildingId { get; set; } | ||
} |