Skip to content

Commit

Permalink
Merge pull request #24 from Purdue-ACM-SIGAPP/add-video-tour
Browse files Browse the repository at this point in the history
Add video tour
  • Loading branch information
AndrewZacharyLiu authored Oct 15, 2024
2 parents 167bc9d + 061b826 commit a518a83
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
94 changes: 94 additions & 0 deletions Controllers/VideoTourController.cs
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

View workflow job for this annotation

GitHub Actions / test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
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);

Check warning on line 63 in Controllers/VideoTourController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
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);

Check warning on line 76 in Controllers/VideoTourController.cs

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
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();
}
}
}

33 changes: 33 additions & 0 deletions Entities/VideoTour.cs
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; }
}

0 comments on commit a518a83

Please sign in to comment.