add CrudController

This commit is contained in:
Фролов 2021-08-02 14:45:13 +05:00
parent 10e7b436c5
commit 4cb797e9c8
9 changed files with 137 additions and 9 deletions

7
AsbCloudApp/Data/IId.cs Normal file
View File

@ -0,0 +1,7 @@
namespace AsbCloudApp.Data
{
public interface IId
{
public int Id { get; set; }
}
}

View File

@ -1,7 +1,7 @@
namespace AsbCloudApp.Data
{
public class WellDto : WellInfoDto, IMapPoint
public class WellDto : WellInfoDto, IMapPoint, IId
{
public int Id { get; set; }
public double? Latitude { get; set; }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace AsbCloudApp.Data
{
public class WellOperationDto
public class WellOperationDto : IId
{
public int Id { get; set; }

View File

@ -1,6 +1,6 @@
namespace AsbCloudApp.Data
{
public class WellSectionDto
public class WellSectionDto: IId
{
public int Id { get; set; }
/// <summary>

View File

@ -3,8 +3,9 @@ using System.Collections.Generic;
namespace AsbCloudApp.Data
{
public class WellStatDto : WellDto
public class WellStatDto : WellDto, IId
{
public int Id { get; set; }
public DateTime? PlanStart { get; set; }
public DateTime? PlanEnd { get; set; }
public DateTime? FactStart { get; set; }

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace AsbCloudApp.Services
{
public interface ICrudService<Tdto>
where Tdto : Data.IId
{
int Delete(int id);
Tdto Get(int id);
IEnumerable<Tdto> GetAll(Expression<Func<object, bool>> predicate = null);
Tdto Insert(Tdto newItem);
IEnumerable<Tdto> InsertRange(IEnumerable<Tdto> newItems);
Tdto Update(Tdto item);
}
}

View File

@ -8,8 +8,9 @@ using Microsoft.Extensions.Configuration;
namespace AsbCloudInfrastructure.Services
{
public class CrudService<Tdto, TModel>
where TModel : class, IId
public class CrudService<Tdto, TModel> : ICrudService<Tdto>
where TModel : class, AsbCloudDb.Model.IId
where Tdto : AsbCloudApp.Data.IId
{
private readonly DbContext context;
private readonly IAsbCloudDbContext db;
@ -21,11 +22,11 @@ namespace AsbCloudInfrastructure.Services
dbSet = context.Set<TModel>();
}
public IEnumerable<Tdto> GetAll(System.Linq.Expressions.Expression< System.Func<TModel, bool>> predicate = null)
public IEnumerable<Tdto> GetAll(System.Linq.Expressions.Expression<System.Func<TModel, bool>> predicate = null)
{
IQueryable<TModel> entities = dbSet;
if(predicate is not null)
if (predicate is not null)
entities = entities.Where(predicate);
var dto = entities.Adapt<Tdto>();
@ -50,7 +51,7 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<Tdto> InsertRange(IEnumerable<Tdto> newItems)
{
var newEntities = newItems.Adapt< IEnumerable<TModel>>();
var newEntities = newItems.Adapt<IEnumerable<TModel>>();
var dbEntities = new Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<TModel>[newItems.Count()];
for (int i = 0; i < dbEntities.Length; i++)

View File

@ -0,0 +1,70 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace AsbCloudWebApi.Controllers
{
[ApiController]
public abstract class CrudController<T> : ControllerBase
where T: IId
{
protected readonly ICrudService<T> service;
public CrudController(ICrudService<T> service)
{
this.service = service;
}
// GET: api/<CrudController>
//[HttpGet]
//public virtual IActionResult GetAll()
//{
// var result = service.GetAll();
// return Ok(result);
//}
// GET api/<CrudController>/5
[HttpGet("{id}")]
public virtual IActionResult Get(int id)
{
var result = service.Get(id);
return Ok(result);
}
// POST api/<CrudController>
[HttpPost]
public virtual IActionResult Insert([FromBody] T value)
{
var result = service.Insert(value);
return Ok(result);
}
//[HttpPost]
//[Route("Range/")]
//public virtual IActionResult InsertRange([FromBody] IEnumerable<T> value)
//{
// var result = service.InsertRange(value);
// return Ok(result);
//}
// PUT api/<CrudController>/5
[HttpPut("{id}")]
public virtual IActionResult Put(int id, [FromBody] T value)
{
var result = service.Update(value);
return Ok(result);
}
// DELETE api/<CrudController>/5
[HttpDelete("{id}")]
public virtual IActionResult Delete(int id)
{
var result = service.Delete(id);
return Ok(result);
}
}
}

View File

@ -0,0 +1,32 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[Route("api/well/{idWell}")]
[ApiController]
[Authorize]
public class WellSectionController : CrudController<WellSectionDto>
{
public WellSectionController(ICrudService<WellSectionDto> service)
:base(service)
{
}
[HttpGet]
[Route("")]
public virtual IActionResult GetAll()
{
var result = service.GetAll();
return Ok(result);
}
}
}