w00t all crud methods completed and working (untested)

This commit is contained in:
Annika
2022-08-02 10:37:47 -06:00
parent d93066c6ca
commit dd93607b5b
11 changed files with 534 additions and 12 deletions
+20 -1
View File
@@ -14,10 +14,12 @@ namespace keepr.Controllers
public class AccountController : ControllerBase public class AccountController : ControllerBase
{ {
private readonly AccountService _accountService; private readonly AccountService _accountService;
private readonly VaultsService _vs;
public AccountController(AccountService accountService) public AccountController(AccountService accountService, VaultsService vs)
{ {
_accountService = accountService; _accountService = accountService;
_vs = vs;
} }
[HttpGet] [HttpGet]
@@ -34,6 +36,23 @@ namespace keepr.Controllers
return BadRequest(e.Message); return BadRequest(e.Message);
} }
} }
[HttpGet("vaults")]
[Authorize]
public async Task<ActionResult<List<Vault>>> GetMyVaults()
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
System.Console.WriteLine(userInfo.Id);
List<Vault> vaults = _vs.GetMyVaults(userInfo.Id);
return Ok(vaults);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
} }
+56
View File
@@ -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<ActionResult<VaultKeep>> Create([FromBody] VaultKeep vaultKeepData)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
vaultKeepData.CreatorId = userInfo.Id;
return _vks.Create(vaultKeepData);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpDelete("{id}")]
[Authorize]
public async Task<ActionResult<int>> Remove(int id)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
VaultKeep vaultKeep = _vks.GetById(id);
vaultKeep.CreatorId = userInfo.Id;
return _vks.Remove(vaultKeep);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
}
}
+107
View File
@@ -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<ActionResult<Vault>> Create([FromBody] Vault vaultData)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
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<ActionResult<Vault>> GetById(int vaultId)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
Vault vault = _vs.GetById(vaultId, userInfo.Id);
return Ok(vault);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("{vaultId}/keeps")]
public async Task<ActionResult<List<Keep>>> GetVaultKeeps(int vaultId)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
Vault vault = _vs.GetById(vaultId, userInfo.Id);
List<Keep> keeps = _vks.GetVaultKeeps(vault.Id);
return Ok(keeps);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpPut("{vaultId}")]
[Authorize]
public async Task<ActionResult<Vault>> Update([FromBody] Vault update, int vaultId)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
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<ActionResult<int>> Remove(int vaultId)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
return Ok(_vs.Remove(vaultId, userInfo.Id));
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
}
}
+2 -2
View File
@@ -9,9 +9,9 @@ namespace keepr.Models
public string CreatorId { get; set; } public string CreatorId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Description { 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; }
} }
+30 -3
View File
@@ -67,9 +67,6 @@ namespace keepr.Repositories
public Keep GetById(int id) public Keep GetById(int id)
{ {
string sql = @" string sql = @"
UPDATE keeps
SET views = views + 1
WHERE id = @id;
SELECT SELECT
a.*, a.*,
k.* k.*
@@ -85,6 +82,16 @@ namespace keepr.Repositories
}, new {id}).FirstOrDefault(); }, 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) public void Update(Keep update)
{ {
string sql = @" string sql = @"
@@ -107,5 +114,25 @@ namespace keepr.Repositories
"; ";
_db.Execute(sql, new {id}); _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});
}
} }
} }
@@ -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<VaultKeep>(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<int>(sql, newVaultKeep);
return newVaultKeep;
}
internal VaultKeep GetById(int id)
{
string sql = @"
SELECT *
FROM vaultKeeps
WHERE id = @Id;
";
return _db.Query<VaultKeep>(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<Keep> 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<Profile, Keep, Keep>(sql, (prof, keep) =>
{
keep.Creator = prof;
return keep;
}, new {vaultId}).ToList();
}
}
}
+66
View File
@@ -31,5 +31,71 @@ namespace keepr.Repositories
AND isPrivate = 0;"; AND isPrivate = 0;";
return _db.Query<Vault>(sql, new {id}).ToList(); return _db.Query<Vault>(sql, new {id}).ToList();
} }
internal List<Vault> GetMyVaults(string id)
{
string sql = @"
SELECT *
FROM vaults
WHERE creatorId = @id;";
return _db.Query<Vault>(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<int>(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<Profile, Vault, Vault>(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;
}
} }
} }
+21
View File
@@ -37,6 +37,12 @@ namespace keepr.Services
} }
internal Keep GetById(int id) internal Keep GetById(int id)
{
_repo.IncViews(id);
return _repo.GetById(id);
}
internal Keep CheckById(int id)
{ {
return _repo.GetById(id); return _repo.GetById(id);
} }
@@ -65,5 +71,20 @@ namespace keepr.Services
_repo.Remove(keepId); _repo.Remove(keepId);
return 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);
}
} }
} }
+76
View File
@@ -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<Keep> GetVaultKeeps(int vaultId)
{
return _repo.GetVaultKeeps(vaultId);
}
}
}
+56
View File
@@ -23,5 +23,61 @@ namespace keepr.Services
{ {
return _repo.GetProfileVaults(id); return _repo.GetProfileVaults(id);
} }
internal List<Vault> 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);
}
} }
} }
+9 -6
View File
@@ -43,14 +43,17 @@ namespace keepr
services.AddScoped<AccountsRepository>(); services.AddScoped<AccountsRepository>();
services.AddScoped<AccountService>(); services.AddScoped<AccountService>();
services.AddScoped<ProfilesRepository>(); services.AddTransient<ProfilesRepository>();
services.AddScoped<ProfilesService>(); services.AddTransient<ProfilesService>();
services.AddScoped<KeepsRepository>(); services.AddTransient<KeepsRepository>();
services.AddScoped<KeepsService>(); services.AddTransient<KeepsService>();
services.AddScoped<VaultsRepository>(); services.AddTransient<VaultsRepository>();
services.AddScoped<VaultsService>(); services.AddTransient<VaultsService>();
services.AddTransient<VaultKeepsRepository>();
services.AddTransient<VaultKeepsService>();
} }
private void ConfigureCors(IServiceCollection services) private void ConfigureCors(IServiceCollection services)