Skip to content

Commit

Permalink
Added CRUD APIs for Encyclopedia Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Dec 1, 2021
1 parent fb0735a commit a83e1b3
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 35 deletions.
144 changes: 140 additions & 4 deletions Library.Encyclopedia.API/Controllers/EncylopediaController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Library.Encyclopedia.DataAccess;
using Library.Encyclopedia.DataAccess.DataAccess;
using Library.Encyclopedia.Entity.Interfaces;
using Library.Encyclopedia.Entity.Models;
using Library.Encyclopedia.Entity.Models.External;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -65,24 +66,119 @@ public async Task<IActionResult> Get(string query,
}
}

/// <summary>
/// Get all items based on category
/// </summary>
/// <param name="query"></param>
/// <param name="offset"></param>
/// <param name="size"></param>
/// <param name="asc"></param>
/// <returns></returns>
[HttpGet("category")]
public async Task<IActionResult> GetByCategory(string category,
int offset = 0,
int size = 10,
bool asc = true)
{
try
{
var response = await mainDataAccess.GetByCategoryAsync(category, offset, size, asc);

if (response == null)
{
return StatusCode(204);
}
else
{
return Ok(response);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

/// <summary>
/// Get all items based on starting alphabet
/// </summary>
/// <param name="query"></param>
/// <param name="offset"></param>
/// <param name="size"></param>
/// <param name="asc"></param>
/// <returns></returns>
[HttpGet("alphabet")]
public async Task<IActionResult> GetByStartingAlphabet(char alphabet,
int offset = 0,
int size = 10,
bool asc = true)
{
try
{
var response = await mainDataAccess.GetByAlphabetAsync(alphabet, offset, size, asc);

if (response == null)
{
return StatusCode(204);
}
else
{
return Ok(response);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

/// <summary>
/// Get an entry by id
/// </summary>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<IActionResult> Get(Guid id)
{
try
{
var response = await mainDataAccess.GetAsync(id);

if (response == null)
{
return StatusCode(204);
}
else
{
return Ok(response);
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

/// <summary>
/// Create new entry
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> Create()
public async Task<IActionResult> Create([FromBody]Main model)
{
try
{
var response = await mainDataAccess.CreateAsync(null);
var response = await mainDataAccess.CreateAsync(model);

if (response == null)
{
return StatusCode(204);
return StatusCode(500);
}
else
{
return Ok();
return Ok(response);
}
}
catch (Exception ex)
Expand All @@ -91,5 +187,45 @@ public async Task<IActionResult> Create()
throw;
}
}

/// <summary>
/// Create new entry
/// </summary>
/// <returns></returns>
[HttpPut("{id}")]
public async Task<IActionResult> Update(Guid id, [FromBody]MainUpdateModel model)
{
try
{
await mainDataAccess.UpdateAsync(id, model);

return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}

/// <summary>
/// Create new entry
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(Guid id)
{
try
{
await mainDataAccess.DeleteAsync(id);

return Ok();
}
catch (Exception ex)
{
_logger.LogError(ex, $"an error has occured {ex.Message}");
throw;
}
}
}
}
122 changes: 102 additions & 20 deletions Library.Encyclopedia.DataAccess/DataAccess/MainDataAccess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Library.Encyclopedia.Entity.Interfaces;
using Library.Encyclopedia.Entity.Exceptions;
using Library.Encyclopedia.Entity.Interfaces;
using Library.Encyclopedia.Entity.Models;
using Library.Encyclopedia.Entity.Models.External;
using Microsoft.EntityFrameworkCore;
Expand All @@ -18,44 +19,125 @@ public MainDataAccess(IApplicationDbContext dbcontext)
_dbcontext = dbcontext;
}



#region GET
async Task<MainMinimizedExternalCollection> IMainDataAccess.GetAsync(string query, int offset, int pagesize, bool ascending)
{
var data = await _dbcontext.Main.Where(s => s.Description.Contains(query) || s.Title.Contains(query))
var temp = _dbcontext.Main.Where(s => s.Description.Contains(query) || s.Title.Contains(query))
.Skip(offset)
.Take(pagesize)
.OrderBy(s => s.Title)
.ThenBy(s => s.Description)
.ToListAsync();

MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data, )
.Take(pagesize);

IEnumerable<Main> data;
if (ascending)
data = await temp.OrderBy(s => s.Title)
.ThenBy(s => s.Description)
.ToListAsync();
else
data = await temp.OrderByDescending(s => s.Title)
.ThenByDescending(s => s.Description)
.ToListAsync();

var total = await _dbcontext.Main.CountAsync(s => s.Description.Contains(query) || s.Title.Contains(query));

MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total);

return result;
}

Task<Main> IMainDataAccess.GetAsync(string id)
async Task<MainMinimizedExternalCollection> IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, bool ascending)
{
throw new NotImplementedException();
var temp = _dbcontext.Main.Where(s => s.Category.Equals(category, StringComparison.OrdinalIgnoreCase))
.Skip(offset)
.Take(pagesize);

IEnumerable<Main> data;
if (ascending)
data = await temp.OrderBy(s => s.Title)
.ThenBy(s => s.Description)
.ToListAsync();
else
data = await temp.OrderByDescending(s => s.Title)
.ThenByDescending(s => s.Description)
.ToListAsync();

var total = await _dbcontext.Main.CountAsync(s => s.Category.Equals(category, StringComparison.OrdinalIgnoreCase));

MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total);

return result;
}

Task<IEnumerable<Main>> IMainDataAccess.GetAsync(char startingAlphabet)
async Task<MainMinimizedExternalCollection> IMainDataAccess.GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending)
{
throw new NotImplementedException();
var temp = _dbcontext.Main.Where(s => s.Title.StartsWith(startingAlphabet.ToString(), StringComparison.OrdinalIgnoreCase))
.Skip(offset)
.Take(pagesize);

IEnumerable<Main> data;
if (ascending)
data = await temp.OrderBy(s => s.Title)
.ThenBy(s => s.Description)
.ToListAsync();
else
data = await temp.OrderByDescending(s => s.Title)
.ThenByDescending(s => s.Description)
.ToListAsync();

var total = await _dbcontext.Main.CountAsync(s => s.Title.StartsWith(startingAlphabet.ToString(), StringComparison.OrdinalIgnoreCase));

MainMinimizedExternalCollection result = new MainMinimizedExternalCollection(data.Minimize(), total);

return result;
}

Task<IEnumerable<Main>> IMainDataAccess.GetByCategoryAsync(string category, int offset, int pagesize, bool ascending)
async Task<Main> IMainDataAccess.GetAsync(Guid id)
{
throw new NotImplementedException();
return await _dbcontext.Main.FirstOrDefaultAsync(s => s.Id == id);
}
#endregion

#region CREATE
public async Task<Guid> CreateAsync(Main main)
public async Task<Guid> CreateAsync(Main model)
{
await _dbcontext.Main.AddAsync(main);
// assign a random id
model.Id = Guid.NewGuid();


await _dbcontext.Main.AddAsync(model);
await _dbcontext.SaveChanges();
return main.Id;
return model.Id;
}
#endregion

#region UPDATE
public async Task UpdateAsync(Guid id, MainUpdateModel model)
{
// find the entry
var entry = await _dbcontext.Main.FirstOrDefaultAsync(s => s.Id == id);

if (entry != null)
{
entry.Title = model.Title;
entry.Description = model.Description;
entry.Category = model.Category;

_dbcontext.Main.Update(entry);
}
else
throw new UpdateFailedException(UpdateFailErrorCode.EntryNotFound);
}
#endregion

#region DELETE
public async Task DeleteAsync(Guid id)
{
// find the entry
var entry = await _dbcontext.Main.FirstOrDefaultAsync(s => s.Id == id);

if (entry != null)
_dbcontext.Main.Remove(entry);
else
throw new DeleteFailedException(DeleteFailErrorCode.EntryNotFound);
}
#endregion
}
}
}
19 changes: 19 additions & 0 deletions Library.Encyclopedia.Entity/Exceptions/DeleteFailedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using EnumsNET;
using System;
using System.ComponentModel;

namespace Library.Encyclopedia.Entity.Exceptions
{
public class DeleteFailedException : Exception
{
public DeleteFailedException(DeleteFailErrorCode code) : base(((DeleteFailErrorCode)code).AsString(EnumFormat.Description))
{
}
}

public enum DeleteFailErrorCode
{
[Description("Entry not found!")]
EntryNotFound
}
}
19 changes: 19 additions & 0 deletions Library.Encyclopedia.Entity/Exceptions/UpdateFailedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using EnumsNET;
using System;
using System.ComponentModel;

namespace Library.Encyclopedia.Entity.Exceptions
{
public class UpdateFailedException : Exception
{
public UpdateFailedException(UpdateFailErrorCode code) : base(((UpdateFailErrorCode)code).AsString(EnumFormat.Description))
{
}
}

public enum UpdateFailErrorCode
{
[Description("Entry not found!")]
EntryNotFound
}
}
15 changes: 11 additions & 4 deletions Library.Encyclopedia.Entity/Interfaces/IMainDataAccess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Library.Encyclopedia.Entity.Models;
using Library.Encyclopedia.Entity.Models.External;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand All @@ -7,10 +8,16 @@ namespace Library.Encyclopedia.Entity.Interfaces
{
public interface IMainDataAccess
{
public Task<IEnumerable<Main>> GetAsync(string query, int offset, int pagesize, bool ascending);
public Task<IEnumerable<Main>> GetByCategoryAsync(string category, int offset, int pagesize, bool ascending);
public Task<Main> GetAsync(string id);
public Task<IEnumerable<Main>> GetAsync(char startingAlphabet);
#region GET
public Task<MainMinimizedExternalCollection> GetAsync(string query, int offset, int pagesize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByCategoryAsync(string category, int offset, int pagesize, bool ascending);
public Task<MainMinimizedExternalCollection> GetByAlphabetAsync(char startingAlphabet, int offset, int pagesize, bool ascending);
public Task<Main> GetAsync(Guid id);
#endregion


public Task<Guid> CreateAsync(Main main);
public Task UpdateAsync(Guid id, MainUpdateModel model);
public Task DeleteAsync(Guid id);
}
}
Loading

0 comments on commit a83e1b3

Please sign in to comment.