keep crud methods complete
This commit is contained in:
@@ -50,5 +50,56 @@ namespace keepr.Controllers
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<Keep> GetById(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
Keep keep = _ks.GetById(id);
|
||||
return Ok(keep);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut("{keepId}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<Keep>> Update([FromBody] Keep keep, int keepId)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
|
||||
keep.CreatorId = userInfo.Id;
|
||||
keep.Id = keepId;
|
||||
keep = _ks.Update(keep);
|
||||
return Ok(keep);
|
||||
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[HttpDelete("{keepId}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<int>> Remove(int keepId)
|
||||
{
|
||||
try
|
||||
{
|
||||
Account userInfo = await HttpContext.GetUserInfoAsync<Account>();
|
||||
int removedId = _ks.Remove(keepId, userInfo.Id);
|
||||
return Ok(removedId);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
return BadRequest(e.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,5 +63,49 @@ namespace keepr.Repositories
|
||||
return keep;
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public Keep GetById(int id)
|
||||
{
|
||||
string sql = @"
|
||||
UPDATE keeps
|
||||
SET views = views + 1
|
||||
WHERE id = @id;
|
||||
SELECT
|
||||
a.*,
|
||||
k.*
|
||||
FROM keeps k
|
||||
JOIN accounts a
|
||||
ON a.id = k.creatorId
|
||||
WHERE k.id = @id;
|
||||
"; // TODO update view counter
|
||||
return _db.Query<Profile, Keep, Keep>(sql, (prof, keep) =>
|
||||
{
|
||||
keep.Creator = prof;
|
||||
return keep;
|
||||
}, new {id}).FirstOrDefault();
|
||||
}
|
||||
|
||||
public void Update(Keep update)
|
||||
{
|
||||
string sql = @"
|
||||
UPDATE keeps
|
||||
SET
|
||||
name = @Name,
|
||||
description = @Description,
|
||||
img = @Img
|
||||
WHERE id = @Id
|
||||
";
|
||||
_db.Execute(sql, update);
|
||||
}
|
||||
|
||||
public void Remove(int id)
|
||||
{
|
||||
string sql = @"
|
||||
DELETE FROM keeps
|
||||
WHERE id = @id
|
||||
LIMIT 1;
|
||||
";
|
||||
_db.Execute(sql, new {id});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,5 +35,35 @@ namespace keepr.Services
|
||||
List<Keep> keeps = _repo.GetAll();
|
||||
return keeps;
|
||||
}
|
||||
|
||||
internal Keep GetById(int id)
|
||||
{
|
||||
return _repo.GetById(id);
|
||||
}
|
||||
|
||||
internal Keep Update(Keep update)
|
||||
{
|
||||
Keep original = GetById(update.Id);
|
||||
if (original.CreatorId != update.CreatorId)
|
||||
{
|
||||
throw new Exception("Nacho Keep!");
|
||||
}
|
||||
original.Name = update.Name ?? original.Name;
|
||||
original.Description = update.Description ?? original.Description;
|
||||
original.Img = update.Img ?? original.Img;
|
||||
_repo.Update(original);
|
||||
return original;
|
||||
}
|
||||
|
||||
internal int Remove(int keepId, string userId)
|
||||
{
|
||||
Keep original = GetById(keepId);
|
||||
if (original.CreatorId != userId)
|
||||
{
|
||||
throw new Exception("Nacho Keep!");
|
||||
}
|
||||
_repo.Remove(keepId);
|
||||
return keepId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,3 +43,13 @@ CREATE TABLE IF NOT EXISTS keeps(
|
||||
FROM vaults
|
||||
WHERE creatorId = '62df16f445a8ad25b2c9485c'
|
||||
AND isPrivate = 0;
|
||||
|
||||
select * from keeps;
|
||||
select * from accounts;
|
||||
|
||||
SELECT
|
||||
a.*,
|
||||
k.*
|
||||
FROM keeps k
|
||||
JOIN accounts a
|
||||
ON a.id = k.creatorId;
|
||||
Reference in New Issue
Block a user