From 4fd65f974ddc34c9ee1c83f7a7844ef8958726ba Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 18 Jun 2016 16:03:33 -0400 Subject: [PATCH] device apis and models --- src/Api/Controllers/DevicesController.cs | 88 +++++++++++++++++++ src/Api/Models/Request/DeviceRequestModel.cs | 36 ++++++++ .../Models/Response/DeviceResponseModel.cs | 28 ++++++ 3 files changed, 152 insertions(+) create mode 100644 src/Api/Controllers/DevicesController.cs create mode 100644 src/Api/Models/Request/DeviceRequestModel.cs create mode 100644 src/Api/Models/Response/DeviceResponseModel.cs diff --git a/src/Api/Controllers/DevicesController.cs b/src/Api/Controllers/DevicesController.cs new file mode 100644 index 0000000000..8e41d83d7a --- /dev/null +++ b/src/Api/Controllers/DevicesController.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Bit.Core.Repositories; +using Microsoft.AspNetCore.Authorization; +using Bit.Api.Models; +using Bit.Core.Exceptions; +using Bit.Core.Domains; +using Microsoft.AspNetCore.Identity; + +namespace Bit.Api.Controllers +{ + [Route("devices")] + [Authorize("Application")] + public class DevicesController : Controller + { + private readonly IDeviceRepository _deviceRepository; + private readonly UserManager _userManager; + + public DevicesController( + IDeviceRepository deviceRepository, + UserManager userManager) + { + _deviceRepository = deviceRepository; + _userManager = userManager; + } + + [HttpGet("{id}")] + public async Task Get(string id) + { + var device = await _deviceRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); + if(device == null) + { + throw new NotFoundException(); + } + + var response = new DeviceResponseModel(device); + return response; + } + + [HttpGet("")] + public async Task> Get() + { + ICollection devices = await _deviceRepository.GetManyByUserIdAsync(new Guid(_userManager.GetUserId(User))); + var responses = devices.Select(d => new DeviceResponseModel(d)); + return new ListResponseModel(responses); + } + + [HttpPost("")] + public async Task Post([FromBody]DeviceRequestModel model) + { + var device = model.ToDevice(_userManager.GetUserId(User)); + await _deviceRepository.CreateAsync(device); + + var response = new DeviceResponseModel(device); + return response; + } + + [HttpPut("{id}")] + public async Task Put(string id, [FromBody]DeviceRequestModel model) + { + var device = await _deviceRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); + if(device == null) + { + throw new NotFoundException(); + } + + await _deviceRepository.ReplaceAsync(model.ToDevice(device)); + + var response = new DeviceResponseModel(device); + return response; + } + + [HttpDelete("{id}")] + public async Task Delete(string id) + { + var device = await _deviceRepository.GetByIdAsync(new Guid(id), new Guid(_userManager.GetUserId(User))); + if(device == null) + { + throw new NotFoundException(); + } + + await _deviceRepository.DeleteAsync(device); + } + } +} diff --git a/src/Api/Models/Request/DeviceRequestModel.cs b/src/Api/Models/Request/DeviceRequestModel.cs new file mode 100644 index 0000000000..af406d8a40 --- /dev/null +++ b/src/Api/Models/Request/DeviceRequestModel.cs @@ -0,0 +1,36 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Bit.Core.Domains; +using Bit.Core.Enums; +using Newtonsoft.Json; + +namespace Bit.Api.Models +{ + public class DeviceRequestModel + { + [Required] + public DeviceType? Type { get; set; } + [Required] + [StringLength(50)] + public string Name { get; set; } + [StringLength(255)] + public string PushToken { get; set; } + + public Device ToDevice(string userId = null) + { + return ToDevice(new Device + { + UserId = new Guid(userId) + }); + } + + public Device ToDevice(Device existingDevice) + { + existingDevice.Name = Name; + existingDevice.PushToken = PushToken; + existingDevice.Type = Type.Value; + + return existingDevice; + } + } +} diff --git a/src/Api/Models/Response/DeviceResponseModel.cs b/src/Api/Models/Response/DeviceResponseModel.cs new file mode 100644 index 0000000000..2a0c4b54ce --- /dev/null +++ b/src/Api/Models/Response/DeviceResponseModel.cs @@ -0,0 +1,28 @@ +using System; +using Bit.Core.Domains; +using Bit.Core.Enums; + +namespace Bit.Api.Models +{ + public class DeviceResponseModel : ResponseModel + { + public DeviceResponseModel(Device device) + : base("device") + { + if(device == null) + { + throw new ArgumentNullException(nameof(device)); + } + + Id = device.Id.ToString(); + Name = device.Name; + Type = device.Type; + CreationDate = device.CreationDate; + } + + public string Id { get; set; } + public string Name { get; set; } + public DeviceType Type { get; set; } + public DateTime CreationDate { get; set; } + } +}