From dd93607b5b743f2cfa204439e7eb069cf0d99f00 Mon Sep 17 00:00:00 2001 From: Annika Date: Tue, 2 Aug 2022 10:37:47 -0600 Subject: [PATCH] w00t all crud methods completed and working (untested) --- keepr/Controllers/AccountController.cs | 21 +++- keepr/Controllers/VaultKeepsController.cs | 56 +++++++++++ keepr/Controllers/VaultsController.cs | 107 +++++++++++++++++++++ keepr/Models/Vault.cs | 4 +- keepr/Repositories/KeepsRepository.cs | 33 ++++++- keepr/Repositories/VaultKeepsRepository.cs | 91 ++++++++++++++++++ keepr/Repositories/VaultsRepository.cs | 66 +++++++++++++ keepr/Services/KeepsService.cs | 21 ++++ keepr/Services/VaultKeepsService.cs | 76 +++++++++++++++ keepr/Services/VaultsService.cs | 56 +++++++++++ keepr/Startup.cs | 15 +-- 11 files changed, 534 insertions(+), 12 deletions(-) create mode 100644 keepr/Controllers/VaultKeepsController.cs create mode 100644 keepr/Controllers/VaultsController.cs create mode 100644 keepr/Repositories/VaultKeepsRepository.cs create mode 100644 keepr/Services/VaultKeepsService.cs diff --git a/keepr/Controllers/AccountController.cs b/keepr/Controllers/AccountController.cs index 59333c8..a68789b 100644 --- a/keepr/Controllers/AccountController.cs +++ b/keepr/Controllers/AccountController.cs @@ -14,10 +14,12 @@ namespace keepr.Controllers public class AccountController : ControllerBase { private readonly AccountService _accountService; + private readonly VaultsService _vs; - public AccountController(AccountService accountService) + public AccountController(AccountService accountService, VaultsService vs) { _accountService = accountService; + _vs = vs; } [HttpGet] @@ -34,6 +36,23 @@ namespace keepr.Controllers return BadRequest(e.Message); } } + + [HttpGet("vaults")] + [Authorize] + public async Task>> GetMyVaults() + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + System.Console.WriteLine(userInfo.Id); + List vaults = _vs.GetMyVaults(userInfo.Id); + return Ok(vaults); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } } diff --git a/keepr/Controllers/VaultKeepsController.cs b/keepr/Controllers/VaultKeepsController.cs new file mode 100644 index 0000000..61002ad --- /dev/null +++ b/keepr/Controllers/VaultKeepsController.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using keepr.Models; +using keepr.Services; +using Microsoft.AspNetCore.Mvc; +using CodeWorks.Auth0Provider; +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; + +namespace keepr.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class VaultKeepsController : ControllerBase + { + private readonly VaultKeepsService _vks; + public VaultKeepsController(VaultKeepsService vks) + { + _vks = vks; + } + + [HttpPost] + [Authorize] + public async Task> Create([FromBody] VaultKeep vaultKeepData) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + vaultKeepData.CreatorId = userInfo.Id; + return _vks.Create(vaultKeepData); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + + [HttpDelete("{id}")] + [Authorize] + public async Task> Remove(int id) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + VaultKeep vaultKeep = _vks.GetById(id); + vaultKeep.CreatorId = userInfo.Id; + return _vks.Remove(vaultKeep); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + } +} \ No newline at end of file diff --git a/keepr/Controllers/VaultsController.cs b/keepr/Controllers/VaultsController.cs new file mode 100644 index 0000000..94e636c --- /dev/null +++ b/keepr/Controllers/VaultsController.cs @@ -0,0 +1,107 @@ +using System.Collections.Generic; +using keepr.Models; +using keepr.Services; +using Microsoft.AspNetCore.Mvc; +using CodeWorks.Auth0Provider; +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; + +namespace keepr.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class VaultsController : ControllerBase + { + private readonly VaultsService _vs; + private readonly VaultKeepsService _vks; + public VaultsController(VaultsService vs, VaultKeepsService vks) + { + _vs = vs; + _vks = vks; + } + + [HttpPost] + [Authorize] + public async Task> Create([FromBody] Vault vaultData) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + vaultData.CreatorId = userInfo.Id; + Vault newVault = _vs.Create(vaultData); + return Ok(newVault); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + + [HttpGet("{vaultId}")] + public async Task> GetById(int vaultId) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + Vault vault = _vs.GetById(vaultId, userInfo.Id); + return Ok(vault); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + + [HttpGet("{vaultId}/keeps")] + public async Task>> GetVaultKeeps(int vaultId) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + Vault vault = _vs.GetById(vaultId, userInfo.Id); + List keeps = _vks.GetVaultKeeps(vault.Id); + return Ok(keeps); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + + [HttpPut("{vaultId}")] + [Authorize] + public async Task> Update([FromBody] Vault update, int vaultId) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + update.Id = vaultId; + update.CreatorId = userInfo.Id; + update = _vs.Update(update); + return Ok(update); + + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + + [HttpDelete("{vaultId}")] + [Authorize] + public async Task> Remove(int vaultId) + { + try + { + Account userInfo = await HttpContext.GetUserInfoAsync(); + return Ok(_vs.Remove(vaultId, userInfo.Id)); + } + catch (System.Exception e) + { + return BadRequest(e.Message); + } + } + } +} \ No newline at end of file diff --git a/keepr/Models/Vault.cs b/keepr/Models/Vault.cs index 0d62f49..19e5f8c 100644 --- a/keepr/Models/Vault.cs +++ b/keepr/Models/Vault.cs @@ -9,9 +9,9 @@ namespace keepr.Models public string CreatorId { get; set; } public string Name { get; set; } public string Description { get; set; } - public bool IsPrivate { get; set; } + public bool? IsPrivate { get; set; } - public Account Creator { get; set; } + public Profile Creator { get; set; } } diff --git a/keepr/Repositories/KeepsRepository.cs b/keepr/Repositories/KeepsRepository.cs index c8e8977..b65c437 100644 --- a/keepr/Repositories/KeepsRepository.cs +++ b/keepr/Repositories/KeepsRepository.cs @@ -67,9 +67,6 @@ namespace keepr.Repositories public Keep GetById(int id) { string sql = @" - UPDATE keeps - SET views = views + 1 - WHERE id = @id; SELECT a.*, k.* @@ -85,6 +82,16 @@ namespace keepr.Repositories }, new {id}).FirstOrDefault(); } + public void IncViews(int id) + { + string sql = @" + UPDATE keeps + SET views = views + 1 + WHERE id = @id; + "; + _db.Execute(sql, new {id}); + } + public void Update(Keep update) { string sql = @" @@ -107,5 +114,25 @@ namespace keepr.Repositories "; _db.Execute(sql, new {id}); } + + public void IncKept(int id) + { + string sql = @" + UPDATE keeps + SET kept = kept + 1 + WHERE id = @id; + "; + _db.Execute(sql, new {id}); + } + + public void DecKept(int id) + { + string sql = @" + UPDATE keeps + SET kept = kept - 1 + WHERE id = @id; + "; + _db.Execute(sql, new {id}); + } } } \ No newline at end of file diff --git a/keepr/Repositories/VaultKeepsRepository.cs b/keepr/Repositories/VaultKeepsRepository.cs new file mode 100644 index 0000000..1974214 --- /dev/null +++ b/keepr/Repositories/VaultKeepsRepository.cs @@ -0,0 +1,91 @@ +using keepr.Controllers; +using keepr.Models; +using keepr.Repositories; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using keepr.Services; +using CodeWorks.Auth0Provider; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Dapper; +using System.Data; +using System.Linq; + +namespace keepr.Repositories +{ + public class VaultKeepsRepository + { + private readonly IDbConnection _db; + public VaultKeepsRepository(IDbConnection db) + { + _db = db; + } + + internal VaultKeep Check(VaultKeep newVaultKeep) + { + string sql = @" + SELECT * + FROM vaultKeeps + WHERE vaultId = @VaultId + AND keepId = @KeepId; + "; + return _db.Query(sql, newVaultKeep).FirstOrDefault(); + } + + internal VaultKeep Create(VaultKeep newVaultKeep) + { + string sql = @" + INSERT INTO vaultKeeps + (creatorId, vaultId, keepId) + VALUES + (@CreatorId, @VaultId, @KeepId); + SELECT LAST_INSERT_ID(); + "; + newVaultKeep.Id = _db.ExecuteScalar(sql, newVaultKeep); + return newVaultKeep; + } + + internal VaultKeep GetById(int id) + { + string sql = @" + SELECT * + FROM vaultKeeps + WHERE id = @Id; + "; + return _db.Query(sql, new {id}).FirstOrDefault(); + } + + internal int Remove(int id) + { + string sql = @" + DELETE FROM vaultKeeps + WHERE id = @id + LIMIT 1; + "; + _db.Execute(sql, new {id}); + return id; + + } + + internal List GetVaultKeeps(int vaultId) + { + string sql = @" + SELECT + a.*, + k.* + FROM keeps k + JOIN accounts a + ON a.id = k.creatorId + JOIN vaultKeeps vk + ON vk.keepId = k.id + WHERE vk.vaultId = @vaultId; + "; + return _db.Query(sql, (prof, keep) => + { + keep.Creator = prof; + return keep; + }, new {vaultId}).ToList(); + } + } +} \ No newline at end of file diff --git a/keepr/Repositories/VaultsRepository.cs b/keepr/Repositories/VaultsRepository.cs index b2a47f3..a2aeba9 100644 --- a/keepr/Repositories/VaultsRepository.cs +++ b/keepr/Repositories/VaultsRepository.cs @@ -31,5 +31,71 @@ namespace keepr.Repositories AND isPrivate = 0;"; return _db.Query(sql, new {id}).ToList(); } + + internal List GetMyVaults(string id) + { + string sql = @" + SELECT * + FROM vaults + WHERE creatorId = @id;"; + return _db.Query(sql, new {id}).ToList(); + } + + internal Vault Create(Vault vaultData) + { + string sql = @" + INSERT INTO vaults + (creatorId, name, description, isPrivate) + VALUES + (@CreatorId, @Name, @Description, @IsPrivate); + SELECT LAST_INSERT_ID(); + "; + vaultData.Id = _db.ExecuteScalar(sql, vaultData); + return vaultData; + + } + + internal Vault GetById(int id) + { + string sql = @" + SELECT + a.*, + v.* + FROM vaults v + JOIN accounts a + ON v.creatorId = a.id + WHERE v.id = @id; + "; + return _db.Query(sql, (prof, vaul) => + { + vaul.Creator = prof; + return vaul; + }, new {id}).FirstOrDefault(); + } + + internal Vault Update(Vault update) + { + string sql = @" + UPDATE vaults + SET + name = @Name, + description = @Description, + isPrivate = @IsPrivate + WHERE id = @Id; + "; + _db.Execute(sql, update); + return update; + } + + internal int Remove(int vaultId) + { + string sql = @" + DELETE FROM vaults + WHERE id = @vaultId + LIMIT 1; + "; + _db.Execute(sql, new {vaultId}); + return vaultId; + } } } \ No newline at end of file diff --git a/keepr/Services/KeepsService.cs b/keepr/Services/KeepsService.cs index c2f6f00..57c6a32 100644 --- a/keepr/Services/KeepsService.cs +++ b/keepr/Services/KeepsService.cs @@ -37,6 +37,12 @@ namespace keepr.Services } internal Keep GetById(int id) + { + _repo.IncViews(id); + return _repo.GetById(id); + } + + internal Keep CheckById(int id) { return _repo.GetById(id); } @@ -65,5 +71,20 @@ namespace keepr.Services _repo.Remove(keepId); return keepId; } + + internal void IncViews(int id) + { + _repo.IncViews(id); + } + + internal void IncKept(int id) + { + _repo.IncKept(id); + } + + internal void DecKept(int id) + { + _repo.DecKept(id); + } } } \ No newline at end of file diff --git a/keepr/Services/VaultKeepsService.cs b/keepr/Services/VaultKeepsService.cs new file mode 100644 index 0000000..e81f773 --- /dev/null +++ b/keepr/Services/VaultKeepsService.cs @@ -0,0 +1,76 @@ +using keepr.Controllers; +using keepr.Models; +using keepr.Repositories; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using keepr.Services; +using CodeWorks.Auth0Provider; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace keepr.Services +{ + public class VaultKeepsService + { + private readonly VaultsService _vs; + private readonly KeepsService _ks; + private readonly VaultKeepsRepository _repo; + public VaultKeepsService(VaultsService vs, KeepsService ks, VaultKeepsRepository repo) + { + _vs = vs; + _ks = ks; + _repo = repo; + } + + internal VaultKeep Create(VaultKeep newVaultKeep) + { + Vault vault = _vs.GetById(newVaultKeep.VaultId, newVaultKeep.CreatorId); + if (vault == null) + { + throw new Exception("No vault for you!"); + } + if (vault.CreatorId != newVaultKeep.CreatorId) + { + throw new Exception("No vault for you!"); + } + Keep keep = _ks.CheckById(newVaultKeep.KeepId); + if (keep == null) + { + throw new Exception("Keep does not exist!"); + } + VaultKeep existingVK = _repo.Check(newVaultKeep); + if (existingVK != null) + { + throw new Exception("Keep is already in vault!"); + } + _ks.IncKept(newVaultKeep.KeepId); + return _repo.Create(newVaultKeep); + } + + internal VaultKeep GetById(int id) + { + return _repo.GetById(id); + } + + internal int Remove(VaultKeep vaultKeep) + { + Vault vault = _vs.GetById(vaultKeep.VaultId, vaultKeep.CreatorId); + if (vault == null) + { + throw new Exception("No vault for you!"); + } + if (vault.CreatorId != vaultKeep.CreatorId) + { + throw new Exception("No vault for you!"); + } + _ks.DecKept(vaultKeep.KeepId); // why no decrement? + return _repo.Remove(vaultKeep.Id); + } + + internal List GetVaultKeeps(int vaultId) + { + return _repo.GetVaultKeeps(vaultId); + } + } +} \ No newline at end of file diff --git a/keepr/Services/VaultsService.cs b/keepr/Services/VaultsService.cs index 245b48d..13a7099 100644 --- a/keepr/Services/VaultsService.cs +++ b/keepr/Services/VaultsService.cs @@ -23,5 +23,61 @@ namespace keepr.Services { return _repo.GetProfileVaults(id); } + + internal List GetMyVaults(string id) + { + return _repo.GetMyVaults(id); + } + + internal Vault Create(Vault original) + { + return _repo.Create(original); + } + + internal Vault GetById(int vaultId, string userId) + { + Vault vault = _repo.GetById(vaultId); + if (vault == null) + { + throw new Exception("No vault for you!"); + } + if (vault.CreatorId != userId && vault.IsPrivate == true) + { + throw new Exception("No vault for you!"); + } + return vault; + } + + internal Vault Update(Vault update) + { + Vault original = GetById(update.Id, update.CreatorId); + if (original == null) + { + throw new Exception("No vault for you!"); + } + if (original.CreatorId != update.CreatorId) + { + throw new Exception("No vault for you!"); + } + original.Name = update.Name ?? original.Name; + original.Description = update.Description ?? original.Description; + original.IsPrivate = update.IsPrivate != null ? update.IsPrivate : original.IsPrivate; + update = _repo.Update(original); + return update; + } + + internal int Remove(int vaultId, string userId) + { + Vault original = GetById(vaultId, userId); + if (original == null) + { + throw new Exception("No vault for you!"); + } + if (original.CreatorId != userId) + { + throw new Exception("No vault for you!"); + } + return _repo.Remove(vaultId); + } } } \ No newline at end of file diff --git a/keepr/Startup.cs b/keepr/Startup.cs index 45ed984..b128fa4 100644 --- a/keepr/Startup.cs +++ b/keepr/Startup.cs @@ -43,14 +43,17 @@ namespace keepr services.AddScoped(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddTransient(); + services.AddTransient(); - services.AddScoped(); - services.AddScoped(); + services.AddTransient(); + services.AddTransient(); - services.AddScoped(); - services.AddScoped(); + services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddTransient(); } private void ConfigureCors(IServiceCollection services)