146 lines
3.9 KiB
C#
146 lines
3.9 KiB
C#
using Church.Net.Entity;
|
|
using Church.Net.Entity.Interface;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using WebAPI.Logics.Interface;
|
|
|
|
namespace WebAPI.Controllers
|
|
{
|
|
[Authorize]
|
|
public class ApiControllerBase<T> : ControllerBase where T : IEntity
|
|
{
|
|
protected readonly ICrudLogic<T> logic;
|
|
|
|
public ApiControllerBase(ICrudLogic<T> logic)
|
|
{
|
|
this.logic = logic;
|
|
}
|
|
|
|
[HttpGet]
|
|
public virtual async Task<IEnumerable<T>> GetAll()
|
|
{
|
|
return await Task.Run(() => { return logic.GetAll(); });
|
|
}
|
|
|
|
[HttpGet]
|
|
public virtual async Task<T> GetById(string id)
|
|
{
|
|
return await Task.Run(() => {
|
|
return logic.GetById(id);
|
|
});
|
|
}
|
|
|
|
[HttpPost]
|
|
public virtual async Task<int> Update(T entity)
|
|
{
|
|
return await Task.Run(() => {
|
|
return logic.Update(entity);
|
|
});
|
|
}
|
|
|
|
[HttpPost]
|
|
public virtual async Task<string> CreateOrUpdate([FromBody] T entity)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
logic.CreateOrUpdate(entity, out string id);
|
|
return id;
|
|
});
|
|
}
|
|
[HttpPost]
|
|
public virtual async Task<IEnumerable<T>> CreateOrUpdateAll([FromBody] IEnumerable<T> entitys)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
foreach (var item in entitys)
|
|
{
|
|
logic.CreateOrUpdate(item, out string id);
|
|
}
|
|
return entitys;
|
|
});
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public virtual async Task<T> Delete(string id)
|
|
{
|
|
return await Task.Run(() => {
|
|
var entity = logic.GetById(id);
|
|
if (entity != null)
|
|
{
|
|
logic.Delete(entity);
|
|
}
|
|
return entity;
|
|
});
|
|
}
|
|
|
|
}
|
|
[Authorize]
|
|
public class CombinedKeyApiControllerBase<T> : ControllerBase where T : ICombinedKeyEntity
|
|
{
|
|
protected readonly ICombinedKeyCrudLogic<T> logic;
|
|
|
|
public CombinedKeyApiControllerBase(ICombinedKeyCrudLogic<T> logic)
|
|
{
|
|
this.logic = logic;
|
|
}
|
|
|
|
[HttpGet]
|
|
public virtual async Task<IEnumerable<T>> GetAll()
|
|
{
|
|
return await Task.Run(() => { return logic.GetAll(); });
|
|
}
|
|
|
|
[HttpGet]
|
|
public virtual async Task<T> GetById(string[] ids)
|
|
{
|
|
return await Task.Run(() => {
|
|
return logic.GetById(ids);
|
|
});
|
|
}
|
|
|
|
[HttpPost]
|
|
public virtual async Task<int> Update(T entity)
|
|
{
|
|
return await Task.Run(() => {
|
|
return logic.Update(entity);
|
|
});
|
|
}
|
|
|
|
[HttpPost]
|
|
public virtual async Task<int> CreateOrUpdate([FromBody] T entity)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
return logic.CreateOrUpdate(entity);
|
|
});
|
|
}
|
|
[HttpPost]
|
|
public virtual async Task<IEnumerable<T>> CreateOrUpdateAll([FromBody] IEnumerable<T> entitys)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
foreach (var item in entitys)
|
|
{
|
|
logic.CreateOrUpdate(item);
|
|
}
|
|
return entitys;
|
|
});
|
|
}
|
|
|
|
[HttpDelete]
|
|
public virtual async Task<T> Delete(string[] ids)
|
|
{
|
|
return await Task.Run(() => {
|
|
var entity = logic.GetById(ids);
|
|
if (entity != null)
|
|
{
|
|
logic.Delete(entity);
|
|
}
|
|
return entity;
|
|
});
|
|
}
|
|
}
|
|
}
|