profile crud complete, keeps, halfway

This commit is contained in:
Annika
2022-08-01 10:17:30 -06:00
parent 3eaefbf02a
commit 48834b8aff
16 changed files with 2601 additions and 4 deletions
Vendored
BIN
View File
Binary file not shown.
File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 245 KiB

@@ -0,0 +1 @@
<!-- <a href="https://www.freepik.com/vectors/404">404 vector created by freepik - www.freepik.com</a> -->
+54
View File
@@ -0,0 +1,54 @@
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 KeepsController : ControllerBase
{
private readonly KeepsService _ks;
public KeepsController(KeepsService ks)
{
_ks = ks;
}
[HttpPost]
[Authorize]
public async Task<ActionResult<Keep>> Create([FromBody] Keep keep)
{
try
{
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
keep.CreatorId = userInfo.Id;
Keep newKeep = _ks.Create(keep);
return Ok(newKeep);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet]
public ActionResult<List<Keep>> GetAll()
{
try
{
List<Keep> keeps = _ks.GetAll();
return Ok(keeps);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
}
}
+67
View File
@@ -0,0 +1,67 @@
using System.Collections.Generic;
using keepr.Models;
using keepr.Services;
using Microsoft.AspNetCore.Mvc;
namespace keepr.Controllers
{
// Establish endpoint path
[ApiController]
[Route("api/[controller]")]
public class ProfilesController : ControllerBase
{
// 'Instantiate' ProfilesService
private readonly ProfilesService _ps;
private readonly KeepsService _ks;
private readonly VaultsService _vs;
public ProfilesController(ProfilesService ps, KeepsService ks, VaultsService vs)
{
_ps = ps;
_ks = ks;
_vs = vs;
}
// Get Profile by Id endpoint
[HttpGet("{id}")]
public ActionResult<Profile> Get(string id)
{
try
{
Profile profile = _ps.Get(id);
return Ok(profile);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("{id}/keeps")]
public ActionResult<List<Keep>> GetKeeps(string id)
{
try
{
List<Keep> keeps = _ks.GetProfileKeeps(id);
return Ok(keeps);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("{id}/vaults")]
public ActionResult<List<Vault>> GetProfileVaults(string id)
{
try
{
List<Vault> vaults = _vs.GetProfileVaults(id);
return Ok(vaults);
}
catch (System.Exception e)
{
return BadRequest(e.Message);
}
}
}
}
+6 -2
View File
@@ -7,17 +7,21 @@ namespace keepr.Models
public class Keep public class Keep
{ {
public int Id { get; set; } public int Id { get; set; }
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 string Img { get; set; } public string Img { get; set; }
public int Views { get; set; } public int Views { get; set; }
public int Kept { get; set; } public int Kept { get; set; }
public Profile Creator { get; set; }
// populate creator? // populate creator?
// shares int - stretch goal // shares int - stretch goal
} }
public class // public class KeepVaultViewModel : Keep
// {
// public int MyProperty { get; set; }
// }
} }
+7
View File
@@ -11,5 +11,12 @@ namespace keepr.Models
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 class VaultKeepViewModel : Vault
{
public Keep VaultKeep { get; set; }
} }
} }
+67
View File
@@ -0,0 +1,67 @@
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 KeepsRepository
{
// Connect to Dapper
private readonly IDbConnection _db;
public KeepsRepository(IDbConnection db)
{
_db = db;
}
public List<Keep> GetProfileKeeps(string id)
{
string sql = @"
SELECT *
FROM keeps
WHERE creatorId = @id;
";
return _db.Query<Keep>(sql, new {id}).ToList();
}
public Keep Create(Keep keep)
{
string sql = @"
INSERT INTO keeps
(creatorId, name, description, img)
VALUES
(@CreatorId, @Name, @Description, @Img);
SELECT LAST_INSERT_ID();
";
keep.Id = _db.ExecuteScalar<int>(sql, keep);
return keep;
}
public List<Keep> GetAll()
{
string sql = @"
SELECT
a.*,
k.*
FROM keeps k
JOIN accounts a
ON a.id = k.creatorId;
";
return _db.Query<Profile, Keep, Keep>(sql, (prof, keep) =>
{
keep.Creator = prof;
return keep;
}).ToList();
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using keepr.Models;
using keepr.Services;
using keepr.Repositories;
using CodeWorks.Auth0Provider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Dapper;
using System.Data;
namespace keepr.Repositories
{
public class ProfilesRepository
{
// Connect to Dapper
private readonly IDbConnection _db;
public ProfilesRepository(IDbConnection db)
{
_db = db;
}
public Profile Get(string id)
{
string sql = @"
SELECT *
FROM accounts
WHERE id = @id;";
return _db.QueryFirstOrDefault<Profile>(sql, new {id});
}
}
}
+35
View File
@@ -0,0 +1,35 @@
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 VaultsRepository
{
private readonly IDbConnection _db;
public VaultsRepository(IDbConnection db)
{
_db = db;
}
internal List<Vault> GetProfileVaults(string id)
{
string sql = @"
SELECT *
FROM vaults
WHERE creatorId = @id
AND isPrivate = 0;";
return _db.Query<Vault>(sql, new {id}).ToList();
}
}
}
+39
View File
@@ -0,0 +1,39 @@
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 KeepsService
{
private readonly KeepsRepository _repo;
public KeepsService(KeepsRepository repo)
{
_repo = repo;
}
internal List<Keep> GetProfileKeeps(string id)
{
return _repo.GetProfileKeeps(id);
}
internal Keep Create(Keep keep)
{
Keep newKeep = _repo.Create(keep);
return newKeep;
}
internal List<Keep> GetAll()
{
List<Keep> keeps = _repo.GetAll();
return keeps;
}
}
}
+32
View File
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using keepr.Models;
using keepr.Services;
using keepr.Repositories;
using CodeWorks.Auth0Provider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace keepr.Services
{
public class ProfilesService
{
// 'Instantiate' ProfilesRepository
private readonly ProfilesRepository _repo;
public ProfilesService(ProfilesRepository repo)
{
_repo = repo;
}
public Profile Get(string id)
{
Profile foundProfile = _repo.Get(id);
if (foundProfile == null)
{
throw new Exception("Profile not found");
}
return foundProfile;
}
}
}
+27
View File
@@ -0,0 +1,27 @@
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 VaultsService
{
private readonly VaultsRepository _repo;
public VaultsService(VaultsRepository repo)
{
_repo = repo;
}
internal List<Vault> GetProfileVaults(string id)
{
return _repo.GetProfileVaults(id);
}
}
}
+9
View File
@@ -42,6 +42,15 @@ namespace keepr
services.AddScoped<AccountsRepository>(); services.AddScoped<AccountsRepository>();
services.AddScoped<AccountService>(); services.AddScoped<AccountService>();
services.AddScoped<ProfilesRepository>();
services.AddScoped<ProfilesService>();
services.AddScoped<KeepsRepository>();
services.AddScoped<KeepsService>();
services.AddScoped<VaultsRepository>();
services.AddScoped<VaultsService>();
} }
private void ConfigureCors(IServiceCollection services) private void ConfigureCors(IServiceCollection services)
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"CONNECTION_STRING": "server=SG-Azula-6401-mysql-master.servers.mongodirector.com;port=3306;database=keepr;user id=keepr;password=sPZ6wy4nbfHj!NE;", "CONNECTION_STRING": "server=SG-Azula-6401-mysql-master.servers.mongodirector.com;port=3306;database=keepr;user id=keepr;password=PWMsucks123!;",
"AUTH0_DOMAIN": "dev-h9kk6tgd.us.auth0.com", "AUTH0_DOMAIN": "dev-h9kk6tgd.us.auth0.com",
"AUTH0_AUDIENCE": "https://AnniDev.com" "AUTH0_AUDIENCE": "https://AnniDev.com"
} }
+37
View File
@@ -6,3 +6,40 @@ CREATE TABLE IF NOT EXISTS accounts(
email varchar(255) COMMENT 'User Email', email varchar(255) COMMENT 'User Email',
picture varchar(255) COMMENT 'User Picture' picture varchar(255) COMMENT 'User Picture'
) default charset utf8 COMMENT ''; ) default charset utf8 COMMENT '';
SELECT * FROM keepr.accounts;
CREATE TABLE IF NOT EXISTS vaultKeeps(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'primary key',
creatorId VARCHAR(255) NOT NULL COMMENT 'id of vault creator',
vaultId INT NOT NULL COMMENT 'ID of vault this keep is in',
keepId INT NOT NULL COMMENT 'ID of keep'
) default charset utf8;
CREATE TABLE IF NOT EXISTS vaults(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'primary key',
creatorId VARCHAR(255) NOT NULL COMMENT 'id of vault creator',
name VARCHAR(255) NOT NULL COMMENT 'name of vault',
description VARCHAR(255) COMMENT 'description of vault'
idPrivate TINYINT DEFAULT 0 COMMENT 'flag for if vault is private'
) default charset utf8;
CREATE TABLE IF NOT EXISTS keeps(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT 'primary key',
creatorId VARCHAR(255) NOT NULL COMMENT 'id of keep creator',
name VARCHAR(255) NOT NULL COMMENT 'name of keep',
description VARCHAR(255) COMMENT 'description of keep',
img VARCHAR(255) DEFAULT 'assets/img/404.svg' COMMENT 'URL of image',
views INT DEFAULT 0 COMMENT 'Number of views',
kept INT DEFAULT 0 COMMENT 'Number of times added to a vault'
) default charset utf8;
SELECT *
FROM accounts
WHERE id = '62df16f445a8ad25b2c9485c';
SELECT *
FROM vaults
WHERE creatorId = '62df16f445a8ad25b2c9485c'
AND isPrivate = 0;