2015-12-08 22:57:38 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2016-05-19 19:10:24 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
using Bit.Core.Repositories;
|
2016-05-19 19:10:24 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2017-03-08 21:55:08 -05:00
|
|
|
|
using Bit.Core.Models.Api;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
using Bit.Core.Exceptions;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
using Bit.Core.Services;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[Route("folders")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class FoldersController : Controller
|
|
|
|
|
|
{
|
2017-03-18 11:35:41 -04:00
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
private readonly ICipherService _cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
private readonly IUserService _userService;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
|
|
|
|
|
public FoldersController(
|
2017-03-18 11:35:41 -04:00
|
|
|
|
IFolderRepository folderRepository,
|
2016-06-29 01:15:37 -04:00
|
|
|
|
ICipherService cipherService,
|
2017-01-24 22:46:54 -05:00
|
|
|
|
IUserService userService)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-03-18 11:35:41 -04:00
|
|
|
|
_folderRepository = folderRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
_cipherService = cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
_userService = userService;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
|
public async Task<FolderResponseModel> Get(string id)
|
|
|
|
|
|
{
|
2017-02-18 01:17:09 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2017-03-18 11:35:41 -04:00
|
|
|
|
var folder = await _folderRepository.GetByIdAsync(new Guid(id), userId);
|
|
|
|
|
|
if(folder == null)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-18 11:35:41 -04:00
|
|
|
|
return new FolderResponseModel(folder);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
2016-05-06 18:23:43 -04:00
|
|
|
|
public async Task<ListResponseModel<FolderResponseModel>> Get()
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-02-18 01:17:09 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2017-03-18 11:35:41 -04:00
|
|
|
|
var folders = await _folderRepository.GetManyByUserIdAsync(userId);
|
|
|
|
|
|
var responses = folders.Select(f => new FolderResponseModel(f));
|
2016-05-06 18:23:43 -04:00
|
|
|
|
return new ListResponseModel<FolderResponseModel>(responses);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("")]
|
|
|
|
|
|
public async Task<FolderResponseModel> Post([FromBody]FolderRequestModel model)
|
|
|
|
|
|
{
|
2017-02-18 01:17:09 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2017-03-18 11:35:41 -04:00
|
|
|
|
var folder = model.ToFolder(_userService.GetProperUserId(User).Value);
|
|
|
|
|
|
await _cipherService.SaveFolderAsync(folder);
|
|
|
|
|
|
return new FolderResponseModel(folder);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}")]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public async Task<FolderResponseModel> Put(string id, [FromBody]FolderRequestModel model)
|
|
|
|
|
|
{
|
2017-02-18 01:17:09 -05:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
2017-03-18 11:35:41 -04:00
|
|
|
|
var folder = await _folderRepository.GetByIdAsync(new Guid(id), userId);
|
|
|
|
|
|
if(folder == null)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
2017-02-18 01:17:09 -05:00
|
|
|
|
|
2017-03-18 11:35:41 -04:00
|
|
|
|
await _cipherService.SaveFolderAsync(model.ToFolder(folder));
|
|
|
|
|
|
return new FolderResponseModel(folder);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}/delete")]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public async Task Delete(string id)
|
|
|
|
|
|
{
|
2017-03-18 11:35:41 -04:00
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
var folder = await _folderRepository.GetByIdAsync(new Guid(id), userId);
|
|
|
|
|
|
if(folder == null)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-18 11:35:41 -04:00
|
|
|
|
await _cipherService.DeleteFolderAsync(folder);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|