From 123e3350fce88fef3760358a86b80d11b721e585 Mon Sep 17 00:00:00 2001 From: Brian Andersen Date: Mon, 7 Aug 2023 18:53:12 +0200 Subject: [PATCH] CHANGE Split timeservice into timeservice and timesheetservice --- .../Controllers/TimesheetController.cs | 23 +++++++-- juultimesedler-be/Interfaces/ITimeService.cs | 12 ++++- .../Interfaces/ITimesheetService.cs | 11 +++++ juultimesedler-be/Program.cs | 4 +- juultimesedler-be/Services/TimeService.cs | 23 ++------- .../Services/TimesheetService.cs | 47 +++++++++++++++++++ 6 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 juultimesedler-be/Interfaces/ITimesheetService.cs create mode 100644 juultimesedler-be/Services/TimesheetService.cs diff --git a/juultimesedler-be/Controllers/TimesheetController.cs b/juultimesedler-be/Controllers/TimesheetController.cs index 2b2d9ac..9b6f72b 100644 --- a/juultimesedler-be/Controllers/TimesheetController.cs +++ b/juultimesedler-be/Controllers/TimesheetController.cs @@ -1,4 +1,5 @@ using juultimesedler_be.DTOs; +using juultimesedler_be.Interfaces; //using juultimesedler_be.Models; using juultimesedler_be.Services; using Microsoft.AspNetCore.Mvc; @@ -9,21 +10,33 @@ namespace juultimesedler_be.Controllers; public class TimesheetController : Controller { private IContentService _contentService; + private ITimesheetService _timesheetService; + private ITimeService _timeService; - public TimesheetController(IContentService contentService) + public TimesheetController(IContentService _contentService, ITimesheetService timesheetService, ITimeService timeService) { - _contentService = contentService; + this._contentService = _contentService; + _timesheetService = timesheetService; + _timeService = timeService; } - [HttpGet("api/gettimesheetweek")] - public async Task GetCurrentTimesheetWeek() + [HttpGet("api/gettimesheetcurrentweek/{WorkerId}")] + public async Task GetCurrentTimesheetWeek(int WorkerId) { TimeService timeService = new(); - GetTimesheetWeekDTO timesheetWeek = timeService.GetCurrentTimesheetWeek(); + GetTimesheetWeekDTO timesheetWeek = _timesheetService.GetTimesheetForCurrentWeek(WorkerId); return timesheetWeek; } + [HttpGet("api/gettimesheetforweek/{WeekNumber}/{WorkerId}")] + public async Task GetTimesheetByWeekNumber(int WeekNumber, int WorkerId) + { + var bp = ""; + GetTimesheetWeekDTO timesheet = _timesheetService.GetTimesheetByWeekNumber(WeekNumber, WorkerId); + return timesheet; + } + [HttpPut("api/puttimesheetweek")] public async Task PutTimesheet([FromBody] PutTimesheetDTO weekTimesheet) { diff --git a/juultimesedler-be/Interfaces/ITimeService.cs b/juultimesedler-be/Interfaces/ITimeService.cs index 2e30dcd..368ec59 100644 --- a/juultimesedler-be/Interfaces/ITimeService.cs +++ b/juultimesedler-be/Interfaces/ITimeService.cs @@ -2,7 +2,17 @@ public interface ITimeService { + /// + /// Get the current weeknumber + /// + /// int + public int GetCurrentWeekNumber(); + /// + /// Get the week dates of the current week + /// + /// + /// Int array of dates. Eks.:[31, 1, 2, 3, 4, 5, 6] + public int[] GetCurrentWeekDates(int weekNumber); public string FormattedCurrentWeek(); - public string FormattedCurrentYearAndWeek(DateTime? date); } diff --git a/juultimesedler-be/Interfaces/ITimesheetService.cs b/juultimesedler-be/Interfaces/ITimesheetService.cs new file mode 100644 index 0000000..9a019de --- /dev/null +++ b/juultimesedler-be/Interfaces/ITimesheetService.cs @@ -0,0 +1,11 @@ +using juultimesedler_be.DTOs; + +namespace juultimesedler_be.Interfaces +{ + public interface ITimesheetService + { + public GetTimesheetWeekDTO GetTimesheetForCurrentWeek(int workerId); + + public GetTimesheetWeekDTO GetTimesheetByWeekNumber(int weekNumber, int workerId); + } +} diff --git a/juultimesedler-be/Program.cs b/juultimesedler-be/Program.cs index 8897896..c13ca73 100644 --- a/juultimesedler-be/Program.cs +++ b/juultimesedler-be/Program.cs @@ -17,6 +17,8 @@ public static IHostBuilder CreateHostBuilder(string[] args) => .ConfigureServices( services => services.AddScoped() + .AddScoped() + .AddScoped() ) .ConfigureUmbracoDefaults() .ConfigureWebHostDefaults(webBuilder => @@ -47,7 +49,7 @@ public void Compose(IUmbracoBuilder builder) builder.Services.AddCors(options => { options.AddDefaultPolicy( - x => x.AllowAnyOrigin() + x => x.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() ); diff --git a/juultimesedler-be/Services/TimeService.cs b/juultimesedler-be/Services/TimeService.cs index 06e22ce..3f4d374 100644 --- a/juultimesedler-be/Services/TimeService.cs +++ b/juultimesedler-be/Services/TimeService.cs @@ -1,12 +1,11 @@ -using juultimesedler_be.DTOs; -using juultimesedler_be.Interfaces; +using juultimesedler_be.Interfaces; using System.Globalization; namespace juultimesedler_be.Services; public class TimeService : ITimeService { - private int WeekNumber() + public int GetCurrentWeekNumber() { CultureInfo cultureInfo = new CultureInfo("da-DK"); Calendar cal = cultureInfo.Calendar; @@ -16,7 +15,7 @@ private int WeekNumber() return weekNumber; } - private int[] WeekDates(int weekNumber) + public int[] GetCurrentWeekDates(int weekNumber) { int[] dates = new int[] { ISOWeek.ToDateTime(DateTime.Now.Year, weekNumber, DayOfWeek.Monday).Day, @@ -46,23 +45,9 @@ public string FormattedCurrentYearAndWeek(DateTime? date = null) string yearNumber = ISOWeek.GetYear((DateTime)date).ToString(); string weekNumber = ISOWeek.GetWeekOfYear((DateTime)date) < 10 - ? "0" + ISOWeek.GetWeekOfYear((DateTime)date).ToString() + ? "0" + ISOWeek.GetWeekOfYear((DateTime)date).ToString() : ISOWeek.GetWeekOfYear((DateTime)date).ToString(); return $"{yearNumber}_{weekNumber}"; } - - public GetTimesheetWeekDTO GetCurrentTimesheetWeek() - { - int weekNumber = WeekNumber(); - GetTimesheetWeekDTO currentTimesheetWeek = new(); - currentTimesheetWeek.WeekNumber = weekNumber; - currentTimesheetWeek.WeekDays = new string[]{ - "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" - }; - currentTimesheetWeek.WeekDates = WeekDates(weekNumber); - - return currentTimesheetWeek; - } - } \ No newline at end of file diff --git a/juultimesedler-be/Services/TimesheetService.cs b/juultimesedler-be/Services/TimesheetService.cs new file mode 100644 index 0000000..8a7da81 --- /dev/null +++ b/juultimesedler-be/Services/TimesheetService.cs @@ -0,0 +1,47 @@ +using juultimesedler_be.DTOs; +using juultimesedler_be.Interfaces; + +namespace juultimesedler_be.Services; + +public class TimesheetService : ITimesheetService +{ + private ITimeService _timeService; + public TimesheetService(ITimeService timeService) + { + _timeService = timeService; + } + + public GetTimesheetWeekDTO GetTimesheetByWeekNumber(int weekNumber, int workerId) + { + int internalWeekNumber = weekNumber; + List demoTimesheets = new List(); + + GetTimesheetWeekDTO demoTimesheet1 = GetTimesheetForCurrentWeek(workerId); + demoTimesheet1.WeekNumber = internalWeekNumber; + demoTimesheets.Add(demoTimesheet1); + + GetTimesheetWeekDTO demoTimesheet2 = GetTimesheetForCurrentWeek(workerId); + demoTimesheet2.WeekNumber = internalWeekNumber - 1; + demoTimesheets.Add(demoTimesheet2); + + GetTimesheetWeekDTO demoTimesheet3 = GetTimesheetForCurrentWeek(workerId); + demoTimesheet3.WeekNumber = internalWeekNumber - 2; + demoTimesheets.Add(demoTimesheet3); + + var result = demoTimesheets.Where(sheet => sheet.WeekNumber == weekNumber).SingleOrDefault(); + + return result; + } + + public GetTimesheetWeekDTO GetTimesheetForCurrentWeek(int workerId) + { + //TODO: Should fetch timesheet from persistance layer (Umb DB) + int weekNumber = _timeService.GetCurrentWeekNumber(); + GetTimesheetWeekDTO currentTimesheetWeek = new(); + currentTimesheetWeek.WeekNumber = weekNumber; + currentTimesheetWeek.WeekDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; + currentTimesheetWeek.WeekDates = _timeService.GetCurrentWeekDates(weekNumber); + + return currentTimesheetWeek; + } +} \ No newline at end of file