diff --git a/HanyadikHetVan/Controllers/PauseController.cs b/HanyadikHetVan/Controllers/PauseController.cs index bf639dd..5067a0e 100644 --- a/HanyadikHetVan/Controllers/PauseController.cs +++ b/HanyadikHetVan/Controllers/PauseController.cs @@ -1,5 +1,4 @@ using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Interface; using HanyadikHetVan.Services; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -8,18 +7,15 @@ using System.Threading.Tasks; namespace HanyadikHetVan.Controllers { - [Route("api/pause")] + [Route("api/pauses")] [ApiController] public class PauseController { private readonly PauseService _pauseService; - private readonly IRepository _pause; - - public PauseController(IRepository pause, PauseService pauseService) + public PauseController(PauseService pauseService) { _pauseService = pauseService; - _pause = pause; } [HttpPost] public async Task AddPause([FromBody] Pause pause) diff --git a/HanyadikHetVan/Controllers/WeeklyTimeSpanController.cs b/HanyadikHetVan/Controllers/WeeklyTimeSpanController.cs index 31f3a7c..85f480e 100644 --- a/HanyadikHetVan/Controllers/WeeklyTimeSpanController.cs +++ b/HanyadikHetVan/Controllers/WeeklyTimeSpanController.cs @@ -1,5 +1,4 @@ using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Interface; using HanyadikHetVan.Services; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; @@ -8,18 +7,15 @@ using System.Threading.Tasks; namespace HanyadikHetVan.Controllers { - [Route("api/weeklytimespan")] + [Route("api/weeklytimespans")] [ApiController] public class WeeklyTimeSpanController { private readonly WeeklyTimeSpanService _weeklytimespanService; - private readonly IRepository _weeklytimespan; - - public WeeklyTimeSpanController(IRepository weeklytimespan, WeeklyTimeSpanService weeklytimespanService) + public WeeklyTimeSpanController(WeeklyTimeSpanService weeklytimespanService) { _weeklytimespanService = weeklytimespanService; - _weeklytimespan = weeklytimespan; } [HttpPost] public async Task AddWeeklyTimeSpan([FromBody] WeeklyTimeSpan weeklytimespan) @@ -61,7 +57,7 @@ namespace HanyadikHetVan.Controllers return false; } } - [HttpGet("{startTime}")] + [HttpGet("date/{startTime}")] public Object GetAllWeeklyTimeSpanByStartdate(DateTime startTime) { var data = _weeklytimespanService.GetWeeklyTimeSpanByStartdate(startTime); @@ -86,6 +82,18 @@ namespace HanyadikHetVan.Controllers return json; } [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) { var data = _weeklytimespanService.GetPausesOfWeeklyTimeSpan(weeklytimespanId); diff --git a/HanyadikHetVan/Interface/IRepository.cs b/HanyadikHetVan/Interface/IRepository.cs deleted file mode 100644 index 545d485..0000000 --- a/HanyadikHetVan/Interface/IRepository.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace HanyadikHetVan.Interface -{ - public interface IRepository - { - public Task Create(T _object); - - public void Update(T _object); - - public IEnumerable GetAll(); - - public T GetById(int Id); - - public void Delete(T _object); - - } -} diff --git a/HanyadikHetVan/Repository/PauseRepository.cs b/HanyadikHetVan/Repository/PauseRepository.cs deleted file mode 100644 index 53f6ca4..0000000 --- a/HanyadikHetVan/Repository/PauseRepository.cs +++ /dev/null @@ -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 - { - private ApplicationDbContext _dbContext; - - public PauseRepository(ApplicationDbContext applicationDbContext) - { - _dbContext = applicationDbContext ?? throw new ArgumentNullException(nameof(applicationDbContext)); - } - - public async Task 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 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(); - } - } -} diff --git a/HanyadikHetVan/Repository/WeeklyTimeSpanRepository.cs b/HanyadikHetVan/Repository/WeeklyTimeSpanRepository.cs deleted file mode 100644 index dcabab9..0000000 --- a/HanyadikHetVan/Repository/WeeklyTimeSpanRepository.cs +++ /dev/null @@ -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 - { - private ApplicationDbContext _dbContext; - - public WeeklyTimeSpanRepository(ApplicationDbContext applicationDbContext) - { - _dbContext = applicationDbContext ?? throw new ArgumentNullException(nameof(applicationDbContext)); - } - - public async Task 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 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(); - } - } -} diff --git a/HanyadikHetVan/Services/PauseService.cs b/HanyadikHetVan/Services/PauseService.cs index 8ba0d18..e78150c 100644 --- a/HanyadikHetVan/Services/PauseService.cs +++ b/HanyadikHetVan/Services/PauseService.cs @@ -1,5 +1,6 @@ -using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Interface; +using HanyadikHetVan.Data; +using HanyadikHetVan.Data.Entities; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -9,29 +10,32 @@ namespace HanyadikHetVan.Services { public class PauseService { - private readonly IRepository _pause; + private readonly ApplicationDbContext _dbContext; - public PauseService(IRepository pause) + public PauseService(ApplicationDbContext dbContext) { - _pause = pause ?? throw new ArgumentNullException(nameof(pause)); + _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); } public IEnumerable GetAllPauses() { - return _pause.GetAll().ToList(); + return _dbContext.Pauses.Include(x => x.WeeklyTimeSpan).ToList(); } public async Task 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) { try { - var item = _pause.GetAll().Where(x => x.Id == pauseId).FirstOrDefault(); - _pause.Delete(item); + var item = _dbContext.Pauses.Where(x => x.Id == pauseId).FirstOrDefault(); + _dbContext.Pauses.Remove(item); + _dbContext.SaveChanges(); return true; } catch (Exception) @@ -44,10 +48,11 @@ namespace HanyadikHetVan.Services { try { - var DataList = _pause.GetAll().ToList(); + var DataList = _dbContext.Pauses.ToList(); foreach (var item in DataList) { - _pause.Update(item); + _dbContext.Pauses.Update(item); + _dbContext.SaveChanges(); } return true; } diff --git a/HanyadikHetVan/Services/WeeklyTimeSpanService.cs b/HanyadikHetVan/Services/WeeklyTimeSpanService.cs index 941be2f..de81ec5 100644 --- a/HanyadikHetVan/Services/WeeklyTimeSpanService.cs +++ b/HanyadikHetVan/Services/WeeklyTimeSpanService.cs @@ -1,7 +1,6 @@ -using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Interface; +using HanyadikHetVan.Data; +using HanyadikHetVan.Data.Entities; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Internal; using System; using System.Collections.Generic; using System.Linq; @@ -11,44 +10,47 @@ namespace HanyadikHetVan.Services { public class WeeklyTimeSpanService { - private readonly IRepository _weeklytimespan; + private readonly ApplicationDbContext _dbContext; - public WeeklyTimeSpanService(IRepository weeklytimespan) + public WeeklyTimeSpanService(ApplicationDbContext dbContext) { - _weeklytimespan = weeklytimespan ?? throw new ArgumentNullException(nameof(weeklytimespan)); + _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); } public IEnumerable GetAllWeeklyTimeSpans() { - return _weeklytimespan.GetAll().ToList(); + return _dbContext.WeeklyTimeSpans.Include(x => x.Pauses).ToList(); } 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 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 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 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) { try { - var item = _weeklytimespan.GetAll().Where(x => x.Id == weeklytimespanId).FirstOrDefault(); - _weeklytimespan.Delete(item); + var item = _dbContext.WeeklyTimeSpans.Where(x => x.Id == weeklytimespanId).FirstOrDefault(); + _dbContext.WeeklyTimeSpans.Remove(item); + _dbContext.SaveChanges(); return true; } catch (Exception) @@ -61,10 +63,11 @@ namespace HanyadikHetVan.Services { try { - var DataList = _weeklytimespan.GetAll().ToList(); + var DataList = _dbContext.WeeklyTimeSpans.ToList(); foreach (var item in DataList) { - _weeklytimespan.Update(item); + _dbContext.WeeklyTimeSpans.Update(item); + _dbContext.SaveChanges(); } return true; } diff --git a/HanyadikHetVan/Startup.cs b/HanyadikHetVan/Startup.cs index 5ac892d..1a9a176 100644 --- a/HanyadikHetVan/Startup.cs +++ b/HanyadikHetVan/Startup.cs @@ -1,7 +1,4 @@ using HanyadikHetVan.Data; -using HanyadikHetVan.Data.Entities; -using HanyadikHetVan.Interface; -using HanyadikHetVan.Repository; using HanyadikHetVan.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -30,8 +27,6 @@ namespace HanyadikHetVan services.AddDbContext(options => options.UseSqlServer( Configuration.GetConnectionString("DefaultConnection"))); - services.AddTransient, PauseRepository>(); - services.AddTransient, WeeklyTimeSpanRepository>(); services.AddTransient(); services.AddTransient(); services.AddTransient();