refactor db access
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Torma Kristóf 2021-05-18 00:53:29 +02:00
parent 4b993aa4e8
commit 0bba2d2faf
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
8 changed files with 51 additions and 161 deletions

View File

@ -1,5 +1,4 @@
using HanyadikHetVan.Data.Entities; using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Interface;
using HanyadikHetVan.Services; using HanyadikHetVan.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -8,18 +7,15 @@ using System.Threading.Tasks;
namespace HanyadikHetVan.Controllers namespace HanyadikHetVan.Controllers
{ {
[Route("api/pause")] [Route("api/pauses")]
[ApiController] [ApiController]
public class PauseController public class PauseController
{ {
private readonly PauseService _pauseService; private readonly PauseService _pauseService;
private readonly IRepository<Pause> _pause; public PauseController(PauseService pauseService)
public PauseController(IRepository<Pause> pause, PauseService pauseService)
{ {
_pauseService = pauseService; _pauseService = pauseService;
_pause = pause;
} }
[HttpPost] [HttpPost]
public async Task<Object> AddPause([FromBody] Pause pause) public async Task<Object> AddPause([FromBody] Pause pause)

View File

@ -1,5 +1,4 @@
using HanyadikHetVan.Data.Entities; using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Interface;
using HanyadikHetVan.Services; using HanyadikHetVan.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -8,18 +7,15 @@ using System.Threading.Tasks;
namespace HanyadikHetVan.Controllers namespace HanyadikHetVan.Controllers
{ {
[Route("api/weeklytimespan")] [Route("api/weeklytimespans")]
[ApiController] [ApiController]
public class WeeklyTimeSpanController public class WeeklyTimeSpanController
{ {
private readonly WeeklyTimeSpanService _weeklytimespanService; private readonly WeeklyTimeSpanService _weeklytimespanService;
private readonly IRepository<WeeklyTimeSpan> _weeklytimespan; public WeeklyTimeSpanController(WeeklyTimeSpanService weeklytimespanService)
public WeeklyTimeSpanController(IRepository<WeeklyTimeSpan> weeklytimespan, WeeklyTimeSpanService weeklytimespanService)
{ {
_weeklytimespanService = weeklytimespanService; _weeklytimespanService = weeklytimespanService;
_weeklytimespan = weeklytimespan;
} }
[HttpPost] [HttpPost]
public async Task<Object> AddWeeklyTimeSpan([FromBody] WeeklyTimeSpan weeklytimespan) public async Task<Object> AddWeeklyTimeSpan([FromBody] WeeklyTimeSpan weeklytimespan)
@ -61,7 +57,7 @@ namespace HanyadikHetVan.Controllers
return false; return false;
} }
} }
[HttpGet("{startTime}")] [HttpGet("date/{startTime}")]
public Object GetAllWeeklyTimeSpanByStartdate(DateTime startTime) public Object GetAllWeeklyTimeSpanByStartdate(DateTime startTime)
{ {
var data = _weeklytimespanService.GetWeeklyTimeSpanByStartdate(startTime); var data = _weeklytimespanService.GetWeeklyTimeSpanByStartdate(startTime);
@ -86,6 +82,18 @@ namespace HanyadikHetVan.Controllers
return json; return json;
} }
[HttpGet("{weeklytimespanId}")] [HttpGet("{weeklytimespanId}")]
public Object GetWeeklyTimeSpanById(int weeklytimespanId)
{
var data = _weeklytimespanService.GetWeeklyTimeSpan(weeklytimespanId);
var json = JsonConvert.SerializeObject(data, Formatting.Indented,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
);
return json;
}
[HttpGet("pauses/{weeklytimespanId}")]
public Object GetAllPausesOfTimeSpan(int weeklytimespanId) public Object GetAllPausesOfTimeSpan(int weeklytimespanId)
{ {
var data = _weeklytimespanService.GetPausesOfWeeklyTimeSpan(weeklytimespanId); var data = _weeklytimespanService.GetPausesOfWeeklyTimeSpan(weeklytimespanId);

View File

@ -1,19 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace HanyadikHetVan.Interface
{
public interface IRepository<T>
{
public Task<T> Create(T _object);
public void Update(T _object);
public IEnumerable<T> GetAll();
public T GetById(int Id);
public void Delete(T _object);
}
}

View File

@ -1,49 +0,0 @@
using HanyadikHetVan.Data;
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HanyadikHetVan.Repository
{
public class PauseRepository : IRepository<Pause>
{
private ApplicationDbContext _dbContext;
public PauseRepository(ApplicationDbContext applicationDbContext)
{
_dbContext = applicationDbContext ?? throw new ArgumentNullException(nameof(applicationDbContext));
}
public async Task<Pause> Create(Pause _object)
{
var obj = await _dbContext.Pauses.AddAsync(_object);
_dbContext.SaveChanges();
return obj.Entity;
}
public void Delete(Pause _object)
{
_dbContext.Remove(_object);
_dbContext.SaveChanges();
}
public IEnumerable<Pause> GetAll()
{
return _dbContext.Pauses.ToList();
}
public Pause GetById(int Id)
{
return _dbContext.Pauses.Where(x => x.Id == Id).FirstOrDefault();
}
public void Update(Pause _object)
{
_dbContext.Pauses.Update(_object);
_dbContext.SaveChanges();
}
}
}

View File

@ -1,49 +0,0 @@
using HanyadikHetVan.Data;
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HanyadikHetVan.Repository
{
public class WeeklyTimeSpanRepository : IRepository<WeeklyTimeSpan>
{
private ApplicationDbContext _dbContext;
public WeeklyTimeSpanRepository(ApplicationDbContext applicationDbContext)
{
_dbContext = applicationDbContext ?? throw new ArgumentNullException(nameof(applicationDbContext));
}
public async Task<WeeklyTimeSpan> Create(WeeklyTimeSpan _object)
{
var obj = await _dbContext.WeeklyTimeSpans.AddAsync(_object);
_dbContext.SaveChanges();
return obj.Entity;
}
public void Delete(WeeklyTimeSpan _object)
{
_dbContext.Remove(_object);
_dbContext.SaveChanges();
}
public IEnumerable<WeeklyTimeSpan> GetAll()
{
return _dbContext.WeeklyTimeSpans.ToList();
}
public WeeklyTimeSpan GetById(int Id)
{
return _dbContext.WeeklyTimeSpans.Where(x => x.Id == Id).FirstOrDefault();
}
public void Update(WeeklyTimeSpan _object)
{
_dbContext.WeeklyTimeSpans.Update(_object);
_dbContext.SaveChanges();
}
}
}

View File

@ -1,5 +1,6 @@
using HanyadikHetVan.Data.Entities; using HanyadikHetVan.Data;
using HanyadikHetVan.Interface; using HanyadikHetVan.Data.Entities;
using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -9,29 +10,32 @@ namespace HanyadikHetVan.Services
{ {
public class PauseService public class PauseService
{ {
private readonly IRepository<Pause> _pause; private readonly ApplicationDbContext _dbContext;
public PauseService(IRepository<Pause> pause) public PauseService(ApplicationDbContext dbContext)
{ {
_pause = pause ?? throw new ArgumentNullException(nameof(pause)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
} }
public IEnumerable<Pause> GetAllPauses() public IEnumerable<Pause> GetAllPauses()
{ {
return _pause.GetAll().ToList(); return _dbContext.Pauses.Include(x => x.WeeklyTimeSpan).ToList();
} }
public async Task<Pause> AddPause(Pause pause) public async Task<Pause> AddPause(Pause pause)
{ {
return await _pause.Create(pause); var obj = await _dbContext.Pauses.AddAsync(pause);
_dbContext.SaveChanges();
return obj.Entity;
} }
public bool DeletePause(int pauseId) public bool DeletePause(int pauseId)
{ {
try try
{ {
var item = _pause.GetAll().Where(x => x.Id == pauseId).FirstOrDefault(); var item = _dbContext.Pauses.Where(x => x.Id == pauseId).FirstOrDefault();
_pause.Delete(item); _dbContext.Pauses.Remove(item);
_dbContext.SaveChanges();
return true; return true;
} }
catch (Exception) catch (Exception)
@ -44,10 +48,11 @@ namespace HanyadikHetVan.Services
{ {
try try
{ {
var DataList = _pause.GetAll().ToList(); var DataList = _dbContext.Pauses.ToList();
foreach (var item in DataList) foreach (var item in DataList)
{ {
_pause.Update(item); _dbContext.Pauses.Update(item);
_dbContext.SaveChanges();
} }
return true; return true;
} }

View File

@ -1,7 +1,6 @@
using HanyadikHetVan.Data.Entities; using HanyadikHetVan.Data;
using HanyadikHetVan.Interface; using HanyadikHetVan.Data.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,44 +10,47 @@ namespace HanyadikHetVan.Services
{ {
public class WeeklyTimeSpanService public class WeeklyTimeSpanService
{ {
private readonly IRepository<WeeklyTimeSpan> _weeklytimespan; private readonly ApplicationDbContext _dbContext;
public WeeklyTimeSpanService(IRepository<WeeklyTimeSpan> weeklytimespan) public WeeklyTimeSpanService(ApplicationDbContext dbContext)
{ {
_weeklytimespan = weeklytimespan ?? throw new ArgumentNullException(nameof(weeklytimespan)); _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
} }
public IEnumerable<WeeklyTimeSpan> GetAllWeeklyTimeSpans() public IEnumerable<WeeklyTimeSpan> GetAllWeeklyTimeSpans()
{ {
return _weeklytimespan.GetAll().ToList(); return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).ToList();
} }
public WeeklyTimeSpan GetWeeklyTimeSpan(int weeklytimespanId) public WeeklyTimeSpan GetWeeklyTimeSpan(int weeklytimespanId)
{ {
return _weeklytimespan.GetAll().Where(x => x.Id == weeklytimespanId).FirstOrDefault(); return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefault();
} }
public ICollection<Pause> GetPausesOfWeeklyTimeSpan(int weeklytimespanId) public ICollection<Pause> GetPausesOfWeeklyTimeSpan(int weeklytimespanId)
{ {
return _weeklytimespan.GetAll().Where(x => x.Id == weeklytimespanId).FirstOrDefault().Pauses; return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Id == weeklytimespanId).FirstOrDefault().Pauses;
} }
public IEnumerable<WeeklyTimeSpan> GetWeeklyTimeSpanByStartdate(DateTime startTime) public IEnumerable<WeeklyTimeSpan> GetWeeklyTimeSpanByStartdate(DateTime startTime)
{ {
return _weeklytimespan.GetAll().Where(x => x.Startdate.Date.Equals(startTime.Date)).ToList(); return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).Where(x => x.Startdate.Date.Equals(startTime.Date)).ToList();
} }
public async Task<WeeklyTimeSpan> AddWeeklyTimeSpan(WeeklyTimeSpan weeklytimespan) public async Task<WeeklyTimeSpan> AddWeeklyTimeSpan(WeeklyTimeSpan weeklytimespan)
{ {
return await _weeklytimespan.Create(weeklytimespan); var obj = await _dbContext.WeeklyTimeSpans.AddAsync(weeklytimespan);
_dbContext.SaveChanges();
return obj.Entity;
} }
public bool DeleteWeeklyTimeSpan(int weeklytimespanId) public bool DeleteWeeklyTimeSpan(int weeklytimespanId)
{ {
try try
{ {
var item = _weeklytimespan.GetAll().Where(x => x.Id == weeklytimespanId).FirstOrDefault(); var item = _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespanId).FirstOrDefault();
_weeklytimespan.Delete(item); _dbContext.WeeklyTimeSpans.Remove(item);
_dbContext.SaveChanges();
return true; return true;
} }
catch (Exception) catch (Exception)
@ -61,10 +63,11 @@ namespace HanyadikHetVan.Services
{ {
try try
{ {
var DataList = _weeklytimespan.GetAll().ToList(); var DataList = _dbContext.WeeklyTimeSpans.ToList();
foreach (var item in DataList) foreach (var item in DataList)
{ {
_weeklytimespan.Update(item); _dbContext.WeeklyTimeSpans.Update(item);
_dbContext.SaveChanges();
} }
return true; return true;
} }

View File

@ -1,7 +1,4 @@
using HanyadikHetVan.Data; using HanyadikHetVan.Data;
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.Interface;
using HanyadikHetVan.Repository;
using HanyadikHetVan.Services; using HanyadikHetVan.Services;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -30,8 +27,6 @@ namespace HanyadikHetVan
services.AddDbContext<ApplicationDbContext>(options => services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer( options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection"))); Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IRepository<Pause>, PauseRepository>();
services.AddTransient<IRepository<WeeklyTimeSpan>, WeeklyTimeSpanRepository>();
services.AddTransient<WeeklyTimeSpanService>(); services.AddTransient<WeeklyTimeSpanService>();
services.AddTransient<PauseService>(); services.AddTransient<PauseService>();
services.AddTransient<HanyadikHetVanJsonService>(); services.AddTransient<HanyadikHetVanJsonService>();