Added endpoint and functionality for getting, retrieving and deleting bookmarks, and for adding and removing tags for a bookmark
This commit is contained in:
103
YABA.API/Controllers/BookmarksController.cs
Normal file
103
YABA.API/Controllers/BookmarksController.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Common.DTOs.Bookmarks;
|
||||
using YABA.Common.DTOs.Tags;
|
||||
using YABA.Service.Interfaces;
|
||||
|
||||
namespace YABA.API.Controllers
|
||||
{
|
||||
[ApiVersion("1")]
|
||||
[Authorize, Route("api/v{version:apiVersion}/[controller]")]
|
||||
public class BookmarksController : ControllerBase
|
||||
{
|
||||
private readonly IBookmarkService _bookmarkService;
|
||||
|
||||
public BookmarksController(IBookmarkService bookmarkService)
|
||||
{
|
||||
_bookmarkService = bookmarkService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(typeof(GenericResponse<CreateBookmarkRequestDTO>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
||||
public async Task<IActionResult> Create(CreateBookmarkRequestDTO request)
|
||||
{
|
||||
var result = await _bookmarkService.CreateBookmark(request);
|
||||
|
||||
if(!result.IsSuccessful) return BadRequest();
|
||||
|
||||
return Ok(new GenericResponse<CreateBookmarkRequestDTO>(result));
|
||||
}
|
||||
|
||||
[HttpPost("{id}/Tags")]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenericResponse<string>>),(int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> UpdateBookmarkTags(int id, IEnumerable<string> tags)
|
||||
{
|
||||
var result = await _bookmarkService.UpdateBookmarkTags(id, tags);
|
||||
|
||||
if (result.All(x => !x.IsSuccessful)) return NotFound();
|
||||
|
||||
return Ok(result.Select(x => new GenericResponse<string>(x)));
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(GenericResponse<IEnumerable<BookmarkDTO>>), (int)HttpStatusCode.OK)]
|
||||
public IActionResult GetAll()
|
||||
{
|
||||
var result = _bookmarkService.GetAll();
|
||||
return Ok(new GenericResponse<IEnumerable<BookmarkDTO>>(result));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(typeof(GenericResponse<BookmarkDTO>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> Get(int id)
|
||||
{
|
||||
var result = await _bookmarkService.Get(id);
|
||||
|
||||
if (!result.IsSuccessful) return NotFound();
|
||||
|
||||
return Ok(new GenericResponse<BookmarkDTO>(result));
|
||||
}
|
||||
|
||||
[HttpGet("{id}/Tags")]
|
||||
[ProducesResponseType(typeof(GenericResponse<IEnumerable<TagSummaryDTO>>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public IActionResult GetBookmarkTags(int id)
|
||||
{
|
||||
var result = _bookmarkService.GetBookmarkTags(id);
|
||||
|
||||
if (!result.IsSuccessful) return NotFound();
|
||||
|
||||
return Ok(new GenericResponse<IEnumerable<TagSummaryDTO>>(result));
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(typeof(GenericResponse<int>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> Delete(int id)
|
||||
{
|
||||
var result = await _bookmarkService.DeleteBookmark(id);
|
||||
|
||||
if (!result.IsSuccessful) return NotFound();
|
||||
|
||||
return Ok(new GenericResponse<int>(result));
|
||||
}
|
||||
|
||||
[HttpDelete()]
|
||||
[ProducesResponseType(typeof(IEnumerable<GenericResponse<int>>), (int)HttpStatusCode.OK)]
|
||||
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
||||
public async Task<IActionResult> DeleteBookmarks(IEnumerable<int> ids)
|
||||
{
|
||||
var result = await _bookmarkService.DeleteBookmarks(ids);
|
||||
|
||||
if(result.All(x => !x.IsSuccessful)) return NotFound();
|
||||
|
||||
return Ok(result.Select((x) => new GenericResponse<int>(x)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using YABA.Common.Extensions;
|
||||
using YABA.Common.Lookups;
|
||||
|
||||
namespace YABA.API.Extensions
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using System.Security.Principal;
|
||||
using YABA.Common.Extensions;
|
||||
using YABA.Common.Lookups;
|
||||
|
||||
namespace YABA.API.Extensions
|
||||
{
|
||||
public static class UserIdentityExtensions
|
||||
{
|
||||
public static string GetAuthProviderId(this IIdentity identity) => GetCustomClaim(identity, ClaimsLookup.AuthProviderId);
|
||||
|
||||
public static string GetCustomClaim(this IIdentity identity, ClaimsLookup claim)
|
||||
{
|
||||
var claimsIdentity = identity as ClaimsIdentity;
|
||||
return claimsIdentity.FindFirst(claim.GetClaimName())?.Value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -43,7 +43,8 @@ builder.Services.AddApiVersioning(setup =>
|
||||
setup.ApiVersionReader = new UrlSegmentApiVersionReader();
|
||||
});
|
||||
|
||||
// Add services to the container.
|
||||
// Add services to the container
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddServiceProjectDependencyInjectionConfiguration(configuration);
|
||||
builder.Services.AddDataProjectDependencyInjectionConfiguration(configuration);
|
||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using AutoMapper;
|
||||
using YABA.API.ViewModels;
|
||||
using YABA.Common.DTOs;
|
||||
using YABA.Common.DTOs.Tags;
|
||||
|
||||
namespace YABA.API.Settings
|
||||
{
|
||||
|
||||
22
YABA.API/ViewModels/Response.cs
Normal file
22
YABA.API/ViewModels/Response.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using YABA.Common.DTOs;
|
||||
using YABA.Common.Extensions;
|
||||
|
||||
namespace YABA.API.ViewModels
|
||||
{
|
||||
public class GenericResponse<T>
|
||||
{
|
||||
public T Entry { get; set; }
|
||||
public int StatusId { get; set; }
|
||||
public string StatusName { get; set; }
|
||||
public string StatusMessage { get; set; }
|
||||
|
||||
public GenericResponse(CrudResultDTO<T> value)
|
||||
{
|
||||
// TODO: Find a way to bring this to AutoMapper
|
||||
Entry = value.Entry;
|
||||
StatusId = (int)value.CrudResult;
|
||||
StatusName = value.CrudResult.ToString();
|
||||
StatusMessage = value.CrudResult.GetDisplayName();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,4 @@
|
||||
using YABA.Service.DTO;
|
||||
|
||||
|
||||
namespace YABA.API.ViewModels
|
||||
{
|
||||
public class UserResponse
|
||||
@ -8,13 +7,5 @@ namespace YABA.API.ViewModels
|
||||
public bool IsDeleted { get; set; }
|
||||
public DateTimeOffset CreatedOn { get; set; }
|
||||
public DateTimeOffset LastModified { get; set; }
|
||||
|
||||
public UserResponse(UserDTO value)
|
||||
{
|
||||
Id = value.Id;
|
||||
IsDeleted = value.IsDeleted;
|
||||
CreatedOn = value.CreatedOn;
|
||||
LastModified = value.LastModified;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user