Skip to content

Commit

Permalink
Backend - Delete request for Product controller. #22
Browse files Browse the repository at this point in the history
  • Loading branch information
julio1927 committed Mar 2, 2021
1 parent aac163e commit 5cf19bc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ECommerceService/ECommerce.Domain/Models/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace ECommerce.Domain
{
public abstract class Entity
{
public virtual int Id { get; protected set; }
public virtual string Id { get; protected set; }
}
}
45 changes: 33 additions & 12 deletions ECommerceService/ECommerce.Infrastructure/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;


namespace ECommerce.Infrastructure
Expand All @@ -11,41 +13,53 @@ public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private MainEcommerceDBContext context;
internal DbSet<T> dbSet = null;

public GenericRepository(MainEcommerceDBContext context)
{
this.context = context ?? throw new ArgumentNullException(nameof(context));
this.dbSet = context.Set<T>();
}
public void Delete(object id)

public async Task<HttpStatusCode> DeleteAsync(object id)
{
T existing = dbSet.Find(id);
if (existing == null)
try
{
throw new ArgumentException(" object doesn't exist.");
T existing = dbSet.Find(id);
if (existing == null)
{
return HttpStatusCode.NotFound;
}

dbSet.Remove(existing);
await context.SaveChangesAsync();
return HttpStatusCode.OK;
}
else
catch
{
dbSet.Remove(existing);
return HttpStatusCode.BadRequest;
}

}

public virtual void Delete(T entity)
{
dbSet.Remove(entity);
}

public virtual IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "")
{
IQueryable<T> query = dbSet;
if(filter != null)
if (filter != null)
{
query = query.Where(filter);
}

foreach(var includeProperty in includeProperties.Split(new char[] { ','}, StringSplitOptions.RemoveEmptyEntries))
foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}

if(orderBy != null)
if (orderBy != null)
{
return orderBy(query).ToList();
}
Expand All @@ -54,43 +68,50 @@ public virtual IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<
return orderBy(query).ToList();
}
}

public virtual IEnumerable<T> GetAll()
{
return dbSet.ToList();
}

public virtual T GetByIdAsync(object id)
{
return dbSet.Find(id);
}

public virtual IEnumerable<T> GetWithSql(string query, params object[] paras)
{
return dbSet.FromSqlRaw<T>(query, paras);
}

public virtual void Insert(T obj)
{
if(obj == null)
if (obj == null)
{
throw new ArgumentException("entity");
}
dbSet.Add(obj);
Save();
}

public virtual void Save()
{
context.SaveChanges();
}

public virtual void Update(T obj)
{
if(obj == null)
if (obj == null)
{
throw new ArgumentException("entity");
}
dbSet.Attach(obj);
context.Entry(obj).State = EntityState.Modified;
}

public bool TryGetObject(object id, out object obj)
{
throw new NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Threading.Tasks;

namespace ECommerce.Infrastructure
{
Expand All @@ -14,7 +16,7 @@ public interface IGenericRepository<T> where T : class
IEnumerable<T> GetWithSql(string query, params object[] paras);
void Insert(T obj);
void Update(T obj);
void Delete(object id);
Task<HttpStatusCode> DeleteAsync(object id);
void Delete(T entity);
void Save();
}
Expand Down
6 changes: 4 additions & 2 deletions ECommerceService/ECommerce.Query/ProductResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

using ECommerce.Domain.Models;

namespace ECommerce.Query
{

// Can be used in Service Layer.
public class ProductResponse<T> : BaseResponse<T>
public class ProductResponse : BaseResponse<Product>
{
public ProductResponse(T product) : base(product) { }
public ProductResponse(Product product) : base(product) { }

public ProductResponse(string message) : base(message) { }
}
Expand Down
17 changes: 17 additions & 0 deletions ECommerceService/ECommerceService/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;

namespace ECommerceService.Controllers
{
Expand Down Expand Up @@ -78,5 +79,21 @@ public ActionResult<Product> PutProduct([FromBody] Product product)
}
}

[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Product))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<string> DeleteAsync(string id)
{
try
{
var HttpMessage = await _productRepository.DeleteAsync(id);
return HttpMessage.ToString();
}
catch (Exception e)
{
// _logger.LogWarning(e, "Unable to PUT product.");
return e.Message;
}
}
}
}

0 comments on commit 5cf19bc

Please sign in to comment.