Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks 1,2 and 6 #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 126 additions & 5 deletions NOS.Engineering.Challenge.API/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Net;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using NOS.Engineering.Challenge.API.Models;
using NOS.Engineering.Challenge.Managers;
using NOS.Engineering.Challenge.Models;

namespace NOS.Engineering.Challenge.API.Controllers;

Expand All @@ -10,9 +12,12 @@ namespace NOS.Engineering.Challenge.API.Controllers;
public class ContentController : Controller
{
private readonly IContentsManager _manager;
public ContentController(IContentsManager manager)
private readonly ILogger<ContentController> _logger;

public ContentController(IContentsManager manager, ILogger<ContentController> logger)
{
_manager = manager;
_logger = logger;
}

[HttpGet]
Expand Down Expand Up @@ -68,20 +73,136 @@ Guid id
}

[HttpPost("{id}/genre")]
public Task<IActionResult> AddGenres(
public async Task<IActionResult> AddGenres(
Guid id,
[FromBody] IEnumerable<string> genre
)
{
return Task.FromResult<IActionResult>(StatusCode((int)HttpStatusCode.NotImplemented));
try
{
// Checks if id exists
var content = await _manager.GetContent(id);

if (content == null)
{
return NotFound();
}

// Checks if provided genres are actually new
var existingGenres = content.GenreList ?? Enumerable.Empty<string>();
var uniqueGenres = genre
.Where( g => !existingGenres.Contains(g, StringComparer.OrdinalIgnoreCase))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();

if (uniqueGenres.Count == 0)
{
return BadRequest("The provided genres are not new");
}

// Updates the content
var updatedContent = await _manager.UpdateContent(id, new ContentDto
(
title: null,
subTitle: null,
description: null,
imageUrl: null,
duration: null,
startTime: null,
endTime: null,
genreList: uniqueGenres
)
);

_logger. LogInformation("Genres added succcessfully to {Id}", id);

return Ok(updatedContent);
}
catch (Exception ex)
{
_logger. LogError(ex, "Error adding genres to {Id}", id);

return StatusCode(500, "Error occured while adding genres");
}
}

[HttpDelete("{id}/genre")]
public Task<IActionResult> RemoveGenres(
public async Task<IActionResult> RemoveGenres(
Guid id,
[FromBody] IEnumerable<string> genre
)
{
return Task.FromResult<IActionResult>(StatusCode((int)HttpStatusCode.NotImplemented));
try
{
// Checks if id exists
var content = await _manager.GetContent(id);

if (content == null)
{
return NotFound();
}

// Updates the content
var updatedGenres = content.GenreList.Except(genre, StringComparer.OrdinalIgnoreCase).ToList();

if(updatedGenres.Count == content.GenreList.Count())
{
return BadRequest ("Provided genres not found");
}
var updatedContent = await _manager.UpdateContent(id, new ContentDto
(
title: null,
subTitle: null,
description: null,
imageUrl: null,
duration: null,
startTime: null,
endTime: null,
genreList: updatedGenres
)
);

_logger. LogInformation("Genres removed successfully from {Id}", id);

return Ok(updatedContent);
}

catch (Exception ex)
{
_logger. LogError(ex, "Error removing genres from {Id}", id);

return StatusCode(500, "Error occurred while removing genres");
}

}


[HttpGet("search")]
public async Task<IActionResult> SearchContents([FromQuery] string? title, [FromQuery] string? genre)
{
try
{
var contents = await _manager.GetManyContents();

if (!string.IsNullOrEmpty(title))
{
contents = contents.Where(c => c.Title.Contains(title, StringComparison.OrdinalIgnoreCase));

}

if(!string.IsNullOrEmpty(genre))
{
contents = contents.Where(c => c.GenreList.Contains(genre, StringComparer.OrdinalIgnoreCase));
}

return Ok(contents);
}

catch (Exception ex)
{
_logger.LogError(ex, "Error searching");

return StatusCode(500, "Error occurred while searching");
}
}
}