From ed250e447a02a659e2ce9fa3bf49d334331bd063 Mon Sep 17 00:00:00 2001 From: HKHemanthsharma Date: Sat, 28 Jun 2025 12:54:17 +0530 Subject: [PATCH 1/7] Project Completed!! --- .../ExerciseTracker.HemanthSharma.sln | 28 +++ .../ContextClass.cs | 25 +++ .../Controllers/ExerciseController.cs | 55 ++++++ .../Controllers/ExerciseShiftController.cs | 72 +++++++ .../DbSettings.cs | 7 + .../ExerciseTracker.HemanthSharma.csproj | 21 ++ .../ExerciseTracker.HemanthSharma.http | 6 + .../HelperMethods/HelperClass.cs | 29 +++ .../Interfaces/IEntity.cs | 7 + .../Models/DTO/ExerciseDto.cs | 7 + .../Models/DTO/ExerciseShiftDto.cs | 16 ++ .../Models/DTO/ResponseDto.cs | 20 ++ .../Models/Exercise.cs | 13 ++ .../Models/ExerciseShift.cs | 16 ++ .../ExerciseTracker.HemanthSharma/Program.cs | 50 +++++ .../Properties/launchSettings.json | 41 ++++ .../Repositories/IRepository.cs | 13 ++ .../Repositories/RepositoryClass.cs | 183 ++++++++++++++++++ .../Repositories/RepositoryDapper.cs | 179 +++++++++++++++++ .../Services/IService.cs | 13 ++ .../Services/ServiceClass.cs | 38 ++++ .../ValidationsMethods/Validations.cs | 16 ++ .../appsettings.Development.json | 8 + .../appsettings.json | 12 ++ .../Exercisetracker.UI.csproj | 15 ++ .../Exercisetracker.UI/HelperMethods.cs | 21 ++ .../Exercisetracker.UI/HttpClientService.cs | 22 +++ .../Interfaces/IExercise.cs | 13 ++ .../Exercisetracker.UI/Interfaces/IShift.cs | 16 ++ .../Exercisetracker.UI/Models/Exercise.cs | 17 ++ .../Exercisetracker.UI/Models/ExerciseDto.cs | 13 ++ .../Models/ExerciseShift.cs | 28 +++ .../Models/ExerciseShiftDto.cs | 26 +++ .../Exercisetracker.UI/Models/ResponseDto.cs | 21 ++ .../Exercisetracker.UI/Program.cs | 13 ++ .../Repositories/Repository.cs | 149 ++++++++++++++ .../Services/ExerciseService.cs | 53 +++++ .../Services/ShiftService.cs | 70 +++++++ .../Exercisetracker.UI/UserInputs.cs | 177 +++++++++++++++++ .../Exercisetracker.UI/UserInterface.cs | 112 +++++++++++ .../Exercisetracker.UI/UserOutputs.cs | 67 +++++++ .../Exercisetracker.UI/Validations.cs | 37 ++++ .../Exercisetracker.UI/app.config | 6 + 43 files changed, 1751 insertions(+) create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.sln create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.csproj create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.http create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseShiftDto.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Properties/launchSettings.json create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.Development.json create mode 100644 ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.json create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Exercisetracker.UI.csproj create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs create mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/app.config diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.sln b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.sln new file mode 100644 index 00000000..e79ca2fc --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35707.178 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExerciseTracker.HemanthSharma", "ExerciseTracker.HemanthSharma\ExerciseTracker.HemanthSharma.csproj", "{5CAB8E83-D604-455F-ACDF-0166CF575818}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercisetracker.UI", "Exercisetracker.UI\Exercisetracker.UI.csproj", "{0517394A-116F-4C1C-BCDA-6CFB1C867BB5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5CAB8E83-D604-455F-ACDF-0166CF575818}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CAB8E83-D604-455F-ACDF-0166CF575818}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CAB8E83-D604-455F-ACDF-0166CF575818}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CAB8E83-D604-455F-ACDF-0166CF575818}.Release|Any CPU.Build.0 = Release|Any CPU + {0517394A-116F-4C1C-BCDA-6CFB1C867BB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0517394A-116F-4C1C-BCDA-6CFB1C867BB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0517394A-116F-4C1C-BCDA-6CFB1C867BB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0517394A-116F-4C1C-BCDA-6CFB1C867BB5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs new file mode 100644 index 00000000..ed1a52de --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs @@ -0,0 +1,25 @@ +using ExerciseTracker.Study.Models; +using Microsoft.EntityFrameworkCore; + +namespace ExerciseTracker.HemanthSharma +{ + public class ContextClass : DbContext + { + public ContextClass(DbContextOptions options) : base(options) { } + public DbSet Exercises { get; set; } + public DbSet ExerciseShifts { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasMany(x => x.ExerciseShifts) + .WithOne(x => x.Exercise) + .HasForeignKey(x => x.ExerciseId); + modelBuilder.Entity() + .HasKey(x => x.Id); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs new file mode 100644 index 00000000..c481cd17 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs @@ -0,0 +1,55 @@ +using ExerciseTracker.Study.Models; +using ExerciseTracker.Study.Models.DTO; +using ExerciseTracker.Study.Services; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace ExerciseTracker.Study.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ExerciseController : ControllerBase + { + private readonly IService Service; + public ExerciseController(IService service) + { + Service = service; + } + [HttpGet] + public async Task>> GetAllExercises() + { + return await Service.GetAll(); + } + [HttpGet] + [Route("{Id:int}")] + public async Task>> GetExerciseById([FromRoute] int Id) + { + return await Service.GetById(Id); + } + [HttpPost] + public async Task>> Create([FromBody] ExerciseDto NewExercise) + { + + return await Service.Create(new Exercise { + Name=NewExercise.Name } + ); + } + [HttpPut] + [Route("{Id:int}")] + public async Task>> Update([FromRoute] int Id,[FromBody] ExerciseDto UpdateExerciseDto) + { + Exercise UpdateExercise = new() + { + Id= Id, + Name=UpdateExerciseDto.Name + }; + return await Service.Update(UpdateExercise); + } + [HttpDelete] + [Route("{Id:int}")] + public async Task>> Delete([FromRoute]int Id) + { + return await Service.Delete(Id); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs new file mode 100644 index 00000000..9dae77ba --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs @@ -0,0 +1,72 @@ +using ExerciseTracker.HemanthSharma.HelperMethods; +using ExerciseTracker.Study.Models; +using ExerciseTracker.Study.Models.DTO; +using ExerciseTracker.Study.Services; +using Microsoft.AspNetCore.Mvc; + +namespace ExerciseTracker.Study.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ExerciseShiftController : ControllerBase + { + private readonly IService ShiftService; + public ExerciseShiftController(IService service) + { + ShiftService = service; + } + [HttpGet] + public async Task>> GetAllShifts() + { + return await ShiftService.GetAll(); + } + [HttpGet] + [Route("{Id:int}")] + public async Task>> GetById([FromRoute] int Id) + { + return await ShiftService.GetById(Id); + } + [HttpPost] + public async Task>> CreateShift([FromBody] ExerciseShiftDto NewExerciseDto) + { + ExerciseShift NewExerciseShift = HelperClass.ConvertToExercise(NewExerciseDto); + if (NewExerciseShift == null) + { + return new ResponseDto + { + IsSuccess = false, + Data = new List(), + Message = "Bad Data! please check if The Shift Date and Times are in correct format", + ResponseMethod = "Post" + }; + + } + return await ShiftService.Create(NewExerciseShift); + } + [HttpPut] + [Route("{Id:int}")] + public async Task>> UpdateShift([FromRoute] int Id, [FromBody] ExerciseShiftDto UpdateExerciseDto) + { + ExerciseShift UpdateExerciseShift = HelperClass.ConvertToExercise(UpdateExerciseDto); + if (UpdateExerciseShift == null) + { + return new ResponseDto + { + IsSuccess = false, + Data = new List(), + Message = "Bad Data! please check if The Shift Date and Times are in correct format", + ResponseMethod = "Post" + }; + } + UpdateExerciseShift.Id = Id; + return await ShiftService.Update(UpdateExerciseShift); + } + [HttpDelete] + [Route("{Id:int}")] + public async Task>> DeleteShift([FromRoute] int Id) + { + + return await ShiftService.Delete(Id); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs new file mode 100644 index 00000000..dd7ea716 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs @@ -0,0 +1,7 @@ +namespace ExerciseTracker.Study +{ + public class DbSettings + { + public string Default { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.csproj b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.csproj new file mode 100644 index 00000000..b1529550 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.http b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.http new file mode 100644 index 00000000..32e933e6 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma.http @@ -0,0 +1,6 @@ +@ExerciseTracker.HemanthSharma_HostAddress = http://localhost:5086 + +GET {{ExerciseTracker.HemanthSharma_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs new file mode 100644 index 00000000..60863cb6 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs @@ -0,0 +1,29 @@ +using ExerciseTracker.Study.Models; +using ExerciseTracker.Study.Models.DTO; +using ExerciseTracker.Study.ValidationsMethods; + +namespace ExerciseTracker.HemanthSharma.HelperMethods +{ + public class HelperClass + { + public static ExerciseShift ConvertToExercise(ExerciseShiftDto ExerciseDto) + { + if (!Validations.ValidTime(ExerciseDto.StartTime) || !Validations.ValidTime(ExerciseDto.EndTime) || !Validations.ValidDate(ExerciseDto.ExerciseDate)) + { + return null; + } + DateTime ShiftStartTime = DateTime.ParseExact(ExerciseDto.StartTime, "HH:mm", null); + DateTime ShiftEndTime = DateTime.ParseExact(ExerciseDto.EndTime, "HH:mm", null); + DateTime ShiftDate = DateTime.ParseExact(ExerciseDto.ExerciseDate, "dd-MM-yyyy", null); + ExerciseShift NewExercise = new ExerciseShift + { + ExerciseId = ExerciseDto.ExerciseId, + StartTime = ShiftStartTime, + EndTime = ShiftEndTime, + Comments = ExerciseDto.Comments, + ExerciseDate = ShiftDate + }; + return NewExercise; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs new file mode 100644 index 00000000..693736b4 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs @@ -0,0 +1,7 @@ +namespace ExerciseTracker.HemanthSharma.Interfaces +{ + public interface IEntity + { + int Id { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs new file mode 100644 index 00000000..b7597565 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs @@ -0,0 +1,7 @@ +namespace ExerciseTracker.Study.Models.DTO +{ + public class ExerciseDto + { + public string Name { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseShiftDto.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseShiftDto.cs new file mode 100644 index 00000000..31422d23 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseShiftDto.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace ExerciseTracker.Study.Models.DTO +{ + public class ExerciseShiftDto + { + public int ExerciseId { get; set; } + [RegularExpression(@"^(0?[0-9]|1[0-9]|2[0-3]):[0-5]\d$")] + public string StartTime { get; set; } + [RegularExpression(@"^(0?[0-9]|1[0-9]|2[0-3]):[0-5]\d$")] + public string EndTime { get; set; } + [RegularExpression(@"^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[0-2])-(19|20)\d{2}$")] + public string ExerciseDate { get; set; } + public string? Comments { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs new file mode 100644 index 00000000..9a4ab8b3 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs @@ -0,0 +1,20 @@ +namespace ExerciseTracker.Study.Models.DTO +{ + public class ResponseDto + { + public bool IsSuccess { get; set; } + public string ResponseMethod { get; set; } + public string Message { get; set; } + public List? Data { get; set; } + public ResponseDto CreateResponse(bool IsSuccess, string Message, string method, List? Data) + { + return new ResponseDto + { + IsSuccess = IsSuccess, + Message = Message, + ResponseMethod = method, + Data = Data + }; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs new file mode 100644 index 00000000..03062b7b --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs @@ -0,0 +1,13 @@ +using ExerciseTracker.HemanthSharma.Interfaces; +using System.Text.Json.Serialization; + +namespace ExerciseTracker.Study.Models +{ + public class Exercise : IEntity + { + public int Id { get; set; } + public string Name { get; set; } + [JsonIgnore] + public List? ExerciseShifts { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs new file mode 100644 index 00000000..af2dd76d --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs @@ -0,0 +1,16 @@ +using ExerciseTracker.HemanthSharma.Interfaces; + +namespace ExerciseTracker.Study.Models +{ + public class ExerciseShift : IEntity + { + public int Id { get; set; } + public int ExerciseId { get; set; } + public DateTime StartTime { get; set; } + public DateTime EndTime { get; set; } + public DateTime ExerciseDate { get; set; } + public int Duration => (int)(EndTime - StartTime).TotalMinutes; + public string? Comments { get; set; } + public Exercise Exercise { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs new file mode 100644 index 00000000..4c28d524 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs @@ -0,0 +1,50 @@ + +using ExerciseTracker.Study; +using ExerciseTracker.Study.Models; +using ExerciseTracker.Study.Repositories; +using ExerciseTracker.Study.Services; +using Microsoft.EntityFrameworkCore; + +namespace ExerciseTracker.HemanthSharma +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Default"))); + builder.Services.AddScoped, RepositoryDapper>(); + // builder.Services.AddScoped, RepositoryClass>(); + builder.Services.AddScoped, RepositoryClass>(); + builder.Services.AddScoped, ServiceClass>(); + //builder.Services.AddScoped, ServiceClass>(); + builder.Services.AddScoped, ServiceClass>(); + builder.Services.Configure(builder.Configuration.GetSection("ConnectionStrings")); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Properties/launchSettings.json b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Properties/launchSettings.json new file mode 100644 index 00000000..08d7e6b3 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:4869", + "sslPort": 44382 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7028;http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs new file mode 100644 index 00000000..c2a47c85 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs @@ -0,0 +1,13 @@ +using ExerciseTracker.Study.Models.DTO; + +namespace ExerciseTracker.Study.Repositories +{ + public interface IRepository + { + Task> GetAll(); + Task> GetById(int Id); + Task> Update(T Entity); + Task> Delete(int Id); + Task> Create(T Entity); + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs new file mode 100644 index 00000000..3b1beae3 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs @@ -0,0 +1,183 @@ +using ExerciseTracker.HemanthSharma; +using ExerciseTracker.HemanthSharma.Interfaces; +using ExerciseTracker.Study.Models.DTO; +using Microsoft.EntityFrameworkCore; + +namespace ExerciseTracker.Study.Repositories +{ + public class RepositoryClass : IRepository where T : class, IEntity + { + private readonly ContextClass Context; + private readonly DbSet DbSet; + public RepositoryClass(ContextClass _context) + { + Context = _context; + DbSet = _context.Set(); + } + public async Task> Create(T Entity) + { + try + { + var EntityResponse = await DbSet.AddAsync(Entity); + await Context.SaveChangesAsync(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "POST", + Message = "Successfully Created The Entity", + Data = new List { EntityResponse.Entity } + }; + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "POST", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + } + + public async Task> Delete(int Id) + { + try + { + var Entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Id); + if (Entity != null) + { + DbSet.Remove(Entity); + Context.SaveChanges(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "DELETE", + Message = "Successfully Deleted The Entities", + Data = [Entity] + }; + } + else + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "DELETE", + Message = "No Entity Found to Delete", + Data = [] + }; + } + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "DELETE", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + + } + + public async Task> GetAll() + { + try + { + var DataList = await DbSet.ToListAsync(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "GET", + Message = "Successfully Fetched All The Entities", + Data = DataList + }; + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + } + + public async Task> GetById(int Id) + { + try + { + var DataEntity = await DbSet.FindAsync(Id); + if (DataEntity == null) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Message = "No Entity Found for given ID", + Data = new List { DataEntity } + }; + } + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "GET", + Message = "Successfully Fetched The Entity With Id", + Data = new List { DataEntity } + }; + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + } + + public async Task> Update(T Entity) + { + try + { + if (await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Entity.Id) != null) + { + DbSet.Update(Entity); + Context.SaveChanges(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "PUT", + Message = "Successfully Updated The Entities", + Data = [Entity] + }; + } + else + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "PUT", + Message = "No Entity Found to Update", + Data = [] + }; + } + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "PUT", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs new file mode 100644 index 00000000..b3529a9f --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs @@ -0,0 +1,179 @@ +using Dapper; +using ExerciseTracker.Study.Models; +using ExerciseTracker.Study.Models.DTO; +using Microsoft.Data.SqlClient; +using Microsoft.Extensions.Options; +using Spectre.Console; +using System.Data.Common; + +namespace ExerciseTracker.Study.Repositories +{ + public class RepositoryDapper : IRepository + { + private string ConnectionString {get;set;} + private DbSettings options { get; set; } + public RepositoryDapper(IOptions _options) + { + options = _options.Value; + ConnectionString = options.Default; + } + + public async Task> Create(Exercise Entity) + { + AnsiConsole.MarkupLine($"[Yellow]Creating The Exercise[darkblue] {Entity.Name}[/] Through[/] [orange3]Dapper[/]"); + try + { + using (SqlConnection conn = new(ConnectionString)) + { + string CreateQuery = "Insert Into Exercises([Name]) Values(@Name);"; + int RowsAffected=conn.Execute(CreateQuery, new { Name = Entity.Name }); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "POST", + Data = [Entity], + Message = "Successfully Created Entity using Dapper" + }; + } + } + catch(Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "POST", + Data = [], + Message = e.Message + }; + } + + + } + + public async Task> Delete(int Id) + { + AnsiConsole.MarkupLine($"[Yellow]Deleting The Exercise[darkblue] [/] Through[/] [orange3]Dapper[/]"); + try + { + using (SqlConnection conn = new(ConnectionString)) + { + string DeleteQuery = "Delete from Exercises where Id=@id ;"; + int RowsAffected = conn.Execute(DeleteQuery, new { id = Id }); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "DELETE", + Data = [], + Message = "Successfully Deleted Entity using Dapper" + }; + } + } + catch (Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "DELETE", + Data = [], + Message = e.Message + }; + } + } + + public async Task> GetAll() + { + try + { + using (SqlConnection conn = new(ConnectionString)) + { + string GetAllQuery = "Select * from Exercises ;"; + List Exercises= conn.Query(GetAllQuery).ToList(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "GET", + Data = Exercises, + Message = "Successfully Retrieved All the Entities using Dapper" + }; + } + } + catch (Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Data = [], + Message = e.Message + }; + } + } + + public async Task> GetById(int Id) + { + try + { + using (SqlConnection conn = new(ConnectionString)) + { + string GetQueryById = "Select * from Exercises where Id=@id ;"; + List Exercises = conn.Query(GetQueryById, new {id=Id}).ToList(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "GET", + Data = Exercises, + Message = "Successfully Retrieved All the Entities using Dapper" + }; + } + } + catch (Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Data = [], + Message = e.Message + }; + } + } + + public async Task> Update(Exercise Entity) + { + try + { + using (SqlConnection conn = new(ConnectionString)) + { + string UpdateQuery = "Update Exercises" + + " set Name=@name where Id=@id;"; + int RowsAffected = conn.Execute(UpdateQuery, new { Name = Entity.Name, id=Entity.Id}); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "PUT", + Data = [Entity], + Message = "Successfully Updated Entity using Dapper" + }; + } + } + catch (Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "PUT", + Data = [], + Message = e.Message + }; + } + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs new file mode 100644 index 00000000..68f6a323 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs @@ -0,0 +1,13 @@ +using ExerciseTracker.Study.Models.DTO; + +namespace ExerciseTracker.Study.Services +{ + public interface IService + { + Task> GetAll(); + Task> GetById(int Id); + Task> Update(T Entity); + Task> Delete(int Id); + Task> Create(T Entity); + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs new file mode 100644 index 00000000..fc6b7533 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs @@ -0,0 +1,38 @@ +using ExerciseTracker.Study.Models.DTO; +using ExerciseTracker.Study.Repositories; + +namespace ExerciseTracker.Study.Services +{ + public class ServiceClass : IService where T:class + { + private readonly IRepository Repo; + public ServiceClass(IRepository _repo) + { + Repo = _repo; + } + public async Task> Create(T Entity) + { + return await Repo.Create(Entity); + } + + public async Task> Delete(int Id) + { + return await Repo.Delete(Id); + } + + public async Task> GetAll() + { + return await Repo.GetAll(); + } + + public async Task> GetById(int Id) + { + return await Repo.GetById(Id); + } + + public async Task> Update(T Entity) + { + return await Repo.Update(Entity); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs new file mode 100644 index 00000000..856e8cd3 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs @@ -0,0 +1,16 @@ +using System.Globalization; + +namespace ExerciseTracker.Study.ValidationsMethods +{ + public class Validations + { + public static bool ValidDate(string Datestring) + { + return DateTime.TryParseExact(Datestring, "dd-MM-yyyy", null, DateTimeStyles.None, out DateTime ShiftDate); + } + public static bool ValidTime(string Timestring) + { + return DateTime.TryParseExact(Timestring, "HH:mm", null, DateTimeStyles.None, out DateTime ShiftDate); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.Development.json b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.json b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.json new file mode 100644 index 00000000..455052f1 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "Default": "Data Source=DESKTOP-3Q1SB5N\\SQLEXPRESS; Integrated Security= true; TrustServerCertificate=true; Initial Catalog=ExerciseTracker;" + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Exercisetracker.UI.csproj b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Exercisetracker.UI.csproj new file mode 100644 index 00000000..1f9406f0 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Exercisetracker.UI.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs new file mode 100644 index 00000000..b68a066a --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs @@ -0,0 +1,21 @@ +using ExerciseTracker.UI.Models; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI +{ + internal static class HelperMethods + { + public static ExerciseShiftDto RefineExerciseShift(ExerciseShiftDto Shift) + { + Shift.StartTime = (DateTime.ParseExact(Shift.StartTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); + Shift.EndTime = (DateTime.ParseExact(Shift.EndTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); + Shift.ExerciseDate = (DateTime.Parse(Shift.ExerciseDate)).ToString("dd-MM-yyyy"); + return Shift; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs new file mode 100644 index 00000000..4e118c0d --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs @@ -0,0 +1,22 @@ +using System.Configuration; + +namespace ExerciseTracker.UI +{ + public class HttpClientService + { + private string BaseUrl { get; set; } + public HttpClientService() + { + BaseUrl = ConfigurationManager.AppSettings["BaseUrl"]; + } + public string GetBaseURL() + { + return BaseUrl; + } + public HttpClient GetHttpClient() + { + HttpClient client = new HttpClient(); + return client; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs new file mode 100644 index 00000000..05bc383f --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Interfaces +{ + public interface IExercise + { + string name { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs new file mode 100644 index 00000000..1011f83f --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Interfaces +{ + public interface IShift + { + int Id { get; set; } + public DateTime StartTime { get; set; } + public DateTime EndTime { get; set; } + public DateTime ExerciseDate { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs new file mode 100644 index 00000000..6bfa8b94 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Models +{ + public class Exercise + { + [property:JsonPropertyName("id")] + public int? id { get; set; } + [property: JsonPropertyName("name")] + public string name { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs new file mode 100644 index 00000000..c58e0519 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Models +{ + public class ExerciseDto + { + public string Name { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs new file mode 100644 index 00000000..304d88b0 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Models +{ + public class ExerciseShift + { + [property: JsonPropertyName("id")] + public int? Id { get; set; } + [property: JsonPropertyName("exerciseId")] + public int ExerciseId { get; set; } + [property: JsonPropertyName("startTime")] + public DateTime StartTime { get; set; } + [property: JsonPropertyName("endTime")] + public DateTime EndTime { get; set; } + [property: JsonPropertyName("exerciseDate")] + public DateTime ExerciseDate { get; set; } + [property: JsonPropertyName("duration")] + public int? Duration { get; set; } + [property: JsonPropertyName("comments")] + public string? Comments { get; set; } + + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs new file mode 100644 index 00000000..a4aa9f98 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Models +{ + public class ExerciseShiftDto + { + [property:JsonPropertyName("id")] + public int? Id { get; set; } + [property: JsonPropertyName("exerciseId")] + public int ExerciseId { get; set; } + [property: JsonPropertyName("startTime")] + public string StartTime { get; set; } + [property: JsonPropertyName("endTime")] + public string EndTime { get; set; } + [property: JsonPropertyName("exerciseDate")] + public string ExerciseDate { get; set; } + [property: JsonPropertyName("comments")] + public string? Comments { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs new file mode 100644 index 00000000..2332761b --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Models +{ + public class ResponseDto where T: class + { + [property:JsonPropertyName("isSuccess")] + public bool IsSuccess { get; set; } + [property: JsonPropertyName("responseMethod")] + public string ResponseMethod { get; set; } + [property: JsonPropertyName("message")] + public string Message { get; set; } + [property: JsonPropertyName("data")] + public List? Data { get; set; } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs new file mode 100644 index 00000000..1c1bf640 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs @@ -0,0 +1,13 @@ +using ExerciseTracker.UI; + +namespace Exercisetracker.UI +{ + internal class Program + { + static void Main(string[] args) + { + bool isRunning = true; + UserInterface.MainMenu(); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs new file mode 100644 index 00000000..10970857 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs @@ -0,0 +1,149 @@ +using ExerciseTracker.UI.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Repositories +{ + public class Repository where T : class + { + public string BaseURL { get; set; } + HttpClientService ClientService = new(); + HttpClient Client; + public Repository() + { + if (typeof(T).Name == "ExerciseShiftDto") + { + BaseURL = ClientService.GetBaseURL() + "ExerciseShift"; + } + else + { + BaseURL = ClientService.GetBaseURL() + typeof(T).Name; + } + Client = ClientService.GetHttpClient(); + } + public async Task> GetAllEntities() + { + try + { + //https://localhost:7249/api/Exercise + //https://localhost:7249/api/ExerciseShift + string Stringresponse = await Client.GetStringAsync(BaseURL); + using (var response = await Client.GetStreamAsync(BaseURL)) + { + ResponseDto GetResponse = JsonSerializer.Deserialize>(response); + return GetResponse; + } + } + catch(Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto + { + IsSuccess=false, + Message=e.Message, + ResponseMethod="GET", + Data = [] + }; + } + } + public async Task> GetEntiryById(int? Id) + { + try + { + using (var response = await Client.GetStreamAsync(BaseURL + $"/{Id}")) + { + ResponseDto GetResponse = JsonSerializer.Deserialize>(response); + return GetResponse; + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto + { + IsSuccess = false, + Message = e.Message, + ResponseMethod = "GET", + Data = [] + }; + } + } + public async Task> CreateEntity(T Entity) + { + try + { + var response = await Client.PostAsJsonAsync(BaseURL, Entity); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) + { + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; + } + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto + { + IsSuccess = false, + Message = e.Message, + ResponseMethod = "POST", + Data = [] + }; + } + } + + public async Task> UpdateEntity(T Entity, int? Id) + { + try + { + var response = await Client.PutAsJsonAsync(BaseURL + $"/{Id}", Entity); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) + { + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto + { + IsSuccess = false, + Message = e.Message, + ResponseMethod = "PUT", + Data = [] + }; + } + } + public async Task> DeleteEntity(int? Id) + { + try + { + var response = await Client.DeleteAsync(BaseURL + $"/{Id}"); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) + { + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto + { + IsSuccess = false, + Message = e.Message, + ResponseMethod = "DELETE", + Data = [] + }; + } + } + + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs new file mode 100644 index 00000000..6685aa5c --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs @@ -0,0 +1,53 @@ +using ExerciseTracker.UI.Models; +using ExerciseTracker.UI.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Services +{ + public class ExerciseService + { + private static readonly Repository Repo= new Repository(); + public static void CreateExercise() + { + Exercise NewExercise = UserInputs.GetNewExercise(); + ResponseDto CreatedExercise= Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(CreatedExercise); + } + + public static void DeleteExercise() + { + List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetExerciseById(Exercises); + ResponseDto DeleteResponse = Repo.DeleteEntity(UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(DeleteResponse); + } + + public static void GetAllExercises() + { + ResponseDto Response = Repo.GetAllEntities().GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); + } + + public static void GetSingleExercise() + { + List Exercises= Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice=UserInputs.GetExerciseById(Exercises); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); + } + + public static void UpdateExercise() + { + List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetExerciseById(Exercises); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + Exercise UpdatedExercise = UserInputs.GetUpdatedEntity(Response.Data); + ResponseDto DeleteResponse = Repo.UpdateEntity(UpdatedExercise, UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs new file mode 100644 index 00000000..f2653824 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs @@ -0,0 +1,70 @@ +using ExerciseTracker.UI.Models; +using ExerciseTracker.UI.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI.Services +{ + public static class ShiftService + { + public static readonly Repository Repo = new Repository(); + public static readonly Repository ExerciseRepo = new Repository(); + public static List GetAvailableExercises() + { + ResponseDto ExercisesResponse = ExerciseRepo.GetAllEntities().GetAwaiter().GetResult(); + return ExercisesResponse.Data; + } + + public static void CreateShift() + { + List AvailableExercises = GetAvailableExercises(); + ExerciseShiftDto NewExercise = UserInputs.GetNewShift(AvailableExercises); + ResponseDto CreateResponse=Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(CreateResponse); + } + + public static void DeleteShift() + { + ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); + int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); + if(SelectedShiftId==-1) + { + return; + } + ResponseDto DeleteShiftRsponse = Repo.DeleteEntity(SelectedShiftId).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(DeleteShiftRsponse); + } + + public static void GetAllShifts() + { + ResponseDto ShiftList=Repo.GetAllEntities().GetAwaiter().GetResult(); + UserOutputs.ShowResponse(ShiftList); + } + + public static void GetSingleShift() + { + ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); + int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); + if(SelectedShiftId==-1) + { + return; + } + ResponseDto ShiftByIdRsponse=Repo.GetEntiryById(SelectedShiftId).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(ShiftByIdRsponse); + } + + public static void UpdateShift() + { + List ExerciseShifts = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetShiftById(ExerciseShifts); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + ExerciseShiftDto UpdatedExerciseShift = UserInputs.GetUpdatedEntity(Response.Data); + UpdatedExerciseShift = HelperMethods.RefineExerciseShift(UpdatedExerciseShift); + ResponseDto UpdateResponse = Repo.UpdateEntity(UpdatedExerciseShift, UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(UpdateResponse); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs new file mode 100644 index 00000000..e9e18e5a --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs @@ -0,0 +1,177 @@ +using ExerciseTracker.UI.Models; +using ExerciseTracker.UI.Repositories; +using Spectre.Console; + +namespace ExerciseTracker.UI +{ + public class UserInputs where T : class + { + internal static int? GetExerciseById(List Entities) + { + List UserChoices = new(); + + UserChoices = Entities.Select(x => x.name).ToList(); + + var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") + .AddChoices(UserChoices)); + return Entities.Where(x => x.name == UserOption).Select(x => x.id).FirstOrDefault(); + } + internal static int GetShiftById(List Entities) + { + if (Entities.Count() == 0) + { + AnsiConsole.MarkupLine("[pink3] No Entities Found!!![/]"); + Console.WriteLine("Click any key to Continue"); + Console.ReadLine(); + return -1; + } + List UserChoices = new(); + UserChoices = Entities.Select(x => $"ShiftId={x.Id};ShiftDate={x.ExerciseDate};ShiftStartTime={x.StartTime}").ToList(); + var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") + .AddChoices(UserChoices)); + return int.Parse(UserOption.Split(";")[0].Split("=")[1]); + } + + internal static Exercise GetNewExercise() + { + string Name = AnsiConsole.Ask("[yellow]Enter the [Blue]Name[/] of the Exercise you want to Add:[/]"); + return new Exercise + { + name = Name + }; + } + + internal static T GetUpdatedEntity(List UpdatedList) + { + T UpdatedEntity = Activator.CreateInstance(); + var props = typeof(T).GetProperties(); + string UpdatedValue = ""; + foreach (var prop in props) + { + string PropertyName = prop.Name; + if (PropertyName.ToLower() != "id") + { + var res = AnsiConsole.Confirm($"[fuchsia]Do you want to change the[yellow] {prop.Name.ToString()}[/] Property:? The Current Value is [aqua]{prop.GetValue(UpdatedList[0])}[/][/]"); + if (res) + { + if (PropertyName.ToLower() == "exerciseid") + { + Repository Repo = new(); + + int? ExerciseId = UserInputs.GetExerciseById(Repo.GetAllEntities().GetAwaiter().GetResult().Data); + UpdatedValue = ExerciseId.ToString(); + + } + else if (PropertyName.ToLower() == "starttime") + { + UpdatedValue = UserInputs.GetShiftTime().ToString("HH:mm"); + } + else if (PropertyName.ToLower() == "endtime") + { + UpdatedValue = UserInputs.GetShiftTime(DateTime.Parse(UpdatedValue)).ToString("HH:mm"); + } + else if (PropertyName.ToLower() == "exercisedate") + { + UpdatedValue = UserInputs.GetShiftDate().ToString("dd-MM-yyyy"); + } + else + { + AnsiConsole.MarkupLine("[olive] Enter the Updated Value:[/]"); + UpdatedValue = Console.ReadLine(); + } + + } + else + { + UpdatedValue = prop.GetValue(UpdatedList[0]).ToString(); + } + if (prop.Name.ToLower() == "exerciseid") + { + prop.SetValue(UpdatedEntity, int.Parse(UpdatedValue)); + } + else + { + prop.SetValue(UpdatedEntity, UpdatedValue); + } + } + } + return UpdatedEntity; + } + + internal static ExerciseShiftDto GetNewShift(List Exercises) + { + AnsiConsole.MarkupLine("[cyan1]Choose the Exercise for which Shift is to be Created:[/]"); + //Table Responsetable = new Table(); + //Responsetable.Title = new TableTitle("[lightseagreen]Available Exercises[/]"); + //Responsetable.AddColumn("Id"); + //Responsetable.AddColumn("Name"); + //foreach (var Exercise in Exercises) + //{ + // Responsetable.AddRow(Exercise.id.ToString(),Exercise.name.ToString()); + //} + //Responsetable.Border = TableBorder.Double; + //AnsiConsole.Write(Responsetable); + List ExerciseNames = Exercises.Select(x => $"Id:{x.id} ExerciseName:{x.name}").ToList(); + string Userchoice = AnsiConsole.Prompt(new SelectionPrompt() + .Title("Please select an Exercise for the Shift:") + .AddChoices(ExerciseNames)); + int ExerciseId = int.Parse(Userchoice.Split(" ")[0].Split(":")[1]); + AnsiConsole.MarkupLine("[yellow3] Enter startTime of Shift[/]"); + DateTime StartTime = GetShiftTime(); + AnsiConsole.MarkupLine("[yellow3] Enter EndTime of Shift[/]"); + DateTime EndTime = GetShiftTime(StartTime); + DateTime ExerciseDate = GetShiftDate(); + AnsiConsole.MarkupLine("comments for this shift?"); + string? Comments = Console.ReadLine(); + return new ExerciseShiftDto + { + ExerciseId = ExerciseId, + StartTime = StartTime.ToString("HH:mm"), + EndTime = EndTime.ToString("HH:mm"), + ExerciseDate = ExerciseDate.ToString("dd-MM-yyyy"), + Comments = Comments + }; + } + + private static DateTime GetShiftDate() + { + bool res = AnsiConsole.Confirm("[orange4] Do you want to Enter Custom date?[/][chartreuse2](Default value will be Today's Date)[/]"); + DateTime ExerciseDate; + if (res) + { + ExerciseDate = Validations.GetValidDate(); + } + else + { + ExerciseDate = DateTime.Now.Date; + } + return ExerciseDate; + } + + private static DateTime GetShiftTime(DateTime? StartTime = null) + { + DateTime StartTimeValue = DateTime.Now; + bool isTimeValid = true; + if (StartTime != null) + { + StartTimeValue = (DateTime)StartTime; + isTimeValid = false; + + } + DateTime TimeResult = Validations.GetValidTime(); + while (!isTimeValid) + { + if (TimeResult >= StartTime) + { + isTimeValid = true; + } + else + { + AnsiConsole.MarkupLine($"[lightgreen]The StartTime of the Shift is {StartTimeValue.ToShortTimeString()} EndDate Can't be before StartTime [/]"); + TimeResult = Validations.GetValidTime(); + } + } + return TimeResult; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs new file mode 100644 index 00000000..51c29eb3 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs @@ -0,0 +1,112 @@ +using ExerciseTracker.UI.Services; +using Spectre.Console; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI +{ + public class UserInterface + { + public static void MainMenu() + { + bool IsAppRunning = true; + while (IsAppRunning) + { + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["Manage Shifts", "Manage Exercises", "Exit"]) + ); + + switch (userOption) + { + case "Manage Shifts": + ShiftServiceMenu(); + break; + case "Manage Exercises": + ExerciseServiceMenu(); + break; + case "Exit": + IsAppRunning = false; + break; + } + } + } + + private static void ExerciseServiceMenu() + { + bool isAppRunning = true; + while (isAppRunning) + { + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["ViewAllExercises", "View a single exercise", "Delete a Exercise", "Create a new Exercise", "Update a Exercise", "Exit"]) + ); + + switch (userOption) + { + case "ViewAllExercises": + ExerciseService.GetAllExercises(); + break; + case "View a single exercise": + ExerciseService.GetSingleExercise(); + break; + case "Delete a Exercise": + ExerciseService.DeleteExercise(); + break; + case "Create a new Exercise": + ExerciseService.CreateExercise(); + break; + case "Update a Exercise": + ExerciseService.UpdateExercise(); + break; + case "Exit": + isAppRunning = false; + break; + } + } + } + + private static void ShiftServiceMenu() + { + bool isAppRunning = true; + while (isAppRunning) + { + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["ViewAllShifts", "View a single Shift", "Delete a shift", "Create a new Shift", "Update a Shift", "Exit"]) + ); + + switch (userOption) + { + case "ViewAllShifts": + ShiftService.GetAllShifts(); + break; + case "View a single Shift": + ShiftService.GetSingleShift(); + break; + case "Delete a shift": + ShiftService.DeleteShift(); + break; + case "Create a new Shift": + ShiftService.CreateShift(); + break; + case "Update a Shift": + ShiftService.UpdateShift(); + break; + case "Exit": + isAppRunning = false; + break; + } + } + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs new file mode 100644 index 00000000..7dde1788 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs @@ -0,0 +1,67 @@ +using ExerciseTracker.UI.Models; +using Spectre.Console; + +namespace ExerciseTracker.UI +{ + public static class UserOutputs where T : class + { + public static void ShowResponse(ResponseDto Response) + { + Console.Clear(); + if (!Response.IsSuccess) + { + string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[maroon]Reponse Message:{Response.Message}[/][/]"; + var panel = new Panel(ResponseString); + panel.Header = new PanelHeader("[Red]Request Failed!!![/]"); + panel.Border = BoxBorder.Rounded; + panel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(panel); + Console.ReadLine(); + } + else + { + string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[green]Reponse Message:{Response.Message}[/][/]"; + var panel = new Panel(ResponseString); + panel.Header = new PanelHeader("[lime]Request Success!!![/]"); + panel.Border = BoxBorder.Rounded; + panel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(panel); + string Heading = Response.ResponseMethod switch + { + "GET" => "Here is the Entity Details", + "POST" => "Details of the Entity Created", + "PUT" => "Details of the updated Entity", + "DELETE" => "Details of the Entity Deleted", + _ => "Unknown" + }; + if (Response.Data.Count() == 0) + { + ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\nCurrently No Data Found For the requested Entityy in DataBase[/]"; + var EmptyMessagePanel = new Panel(ResponseString); + EmptyMessagePanel.Header = new PanelHeader("[lime]Empty Data!!![/]"); + EmptyMessagePanel.Border = BoxBorder.Rounded; + EmptyMessagePanel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(EmptyMessagePanel); + Console.ReadLine(); + } + else + { + AnsiConsole.MarkupLine(Heading); + Table Responsetable = new Table(); + Responsetable.Title = new TableTitle(typeof(T).Name); + var props = typeof(T).GetProperties().ToList(); ; + props.ForEach(x => Responsetable.AddColumn(Markup.Escape(x.Name))); + foreach (var ResponseObject in Response.Data) + { + List RowData = new(); + props.ForEach(x => RowData.Add(x.GetValue(ResponseObject).ToString())); + Responsetable.AddRow(RowData.ToArray()); + } + Responsetable.Border = TableBorder.Double; + AnsiConsole.Write(Responsetable); + } + } + Console.ReadLine(); + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs new file mode 100644 index 00000000..84e00d47 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs @@ -0,0 +1,37 @@ +using Spectre.Console; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ExerciseTracker.UI +{ + internal class Validations + { + internal static DateTime GetValidDate() + { + AnsiConsole.MarkupLine("[darkred]Enter the Date in 'dd-MM-yyyy' Format:[/]"); + bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateTime DateResult); + while (!res) + { + AnsiConsole.MarkupLine("[darkred]Enter the Date in correct [yellow] 'dd-MM-yyyy'[/] Format:[/]"); + res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateResult); + } + return DateResult; + } + + internal static DateTime GetValidTime() + { + AnsiConsole.MarkupLine("[lightsteelblue]Enter the Time in 'HH:mm' Format:[/]"); + bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out DateTime TimeResult); + while (!res) + { + AnsiConsole.MarkupLine("[red3_1]Enter the Time in correct [yellow] 'HH:mm'[/] Format:[/]"); + res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out TimeResult); + } + return TimeResult; + } + } +} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/app.config b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/app.config new file mode 100644 index 00000000..9f4162fd --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/app.config @@ -0,0 +1,6 @@ + + + + + + From 6e4c87fbff5dffeb81d7a5340e60c8d7ea542709 Mon Sep 17 00:00:00 2001 From: HKHemanthsharma Date: Sat, 28 Jun 2025 13:27:13 +0530 Subject: [PATCH 2/7] Removed unused using Statements --- .../ContextClass.cs | 36 +-- .../Controllers/ExerciseController.cs | 84 +++--- .../Controllers/ExerciseShiftController.cs | 107 ++++---- .../DbSettings.cs | 6 +- .../HelperMethods/HelperClass.cs | 37 ++- .../Interfaces/IEntity.cs | 9 +- .../Models/DTO/ExerciseDto.cs | 9 +- .../Models/DTO/ResponseDto.cs | 29 +- .../Models/Exercise.cs | 15 +- .../Models/ExerciseShift.cs | 23 +- .../ExerciseTracker.HemanthSharma/Program.cs | 58 ++-- .../Repositories/IRepository.cs | 17 +- .../Repositories/RepositoryClass.cs | 245 +++++++++-------- .../Repositories/RepositoryDapper.cs | 242 +++++++++-------- .../Services/IService.cs | 17 +- .../Services/ServiceClass.cs | 53 ++-- .../ValidationsMethods/Validations.cs | 6 +- .../Exercisetracker.UI/HelperMethods.cs | 22 +- .../Exercisetracker.UI/HttpClientService.cs | 31 ++- .../Interfaces/IExercise.cs | 13 - .../Exercisetracker.UI/Interfaces/IShift.cs | 16 -- .../Exercisetracker.UI/Models/Exercise.cs | 22 +- .../Exercisetracker.UI/Models/ExerciseDto.cs | 13 +- .../Models/ExerciseShift.cs | 44 ++- .../Models/ExerciseShiftDto.cs | 39 ++- .../Exercisetracker.UI/Models/ResponseDto.cs | 30 +-- .../Exercisetracker.UI/Program.cs | 13 +- .../Repositories/Repository.cs | 222 ++++++++------- .../Services/ExerciseService.cs | 78 +++--- .../Services/ShiftService.cs | 104 ++++--- .../Exercisetracker.UI/UserInputs.cs | 255 +++++++++--------- .../Exercisetracker.UI/UserInterface.cs | 176 ++++++------ .../Exercisetracker.UI/UserOutputs.cs | 99 ++++--- .../Exercisetracker.UI/Validations.cs | 44 ++- 34 files changed, 1044 insertions(+), 1170 deletions(-) delete mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs delete mode 100644 ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs index ed1a52de..daa0fbc8 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ContextClass.cs @@ -1,25 +1,25 @@ using ExerciseTracker.Study.Models; using Microsoft.EntityFrameworkCore; -namespace ExerciseTracker.HemanthSharma +namespace ExerciseTracker.HemanthSharma; + +public class ContextClass : DbContext { - public class ContextClass : DbContext - { - public ContextClass(DbContextOptions options) : base(options) { } - public DbSet Exercises { get; set; } - public DbSet ExerciseShifts { get; set; } + public ContextClass(DbContextOptions options) : base(options) { } + public DbSet Exercises { get; set; } + public DbSet ExerciseShifts { get; set; } - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.Entity() - .HasKey(x => x.Id); - modelBuilder.Entity() - .HasMany(x => x.ExerciseShifts) - .WithOne(x => x.Exercise) - .HasForeignKey(x => x.ExerciseId); - modelBuilder.Entity() - .HasKey(x => x.Id); - } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + modelBuilder.Entity() + .HasKey(x => x.Id); + modelBuilder.Entity() + .HasMany(x => x.ExerciseShifts) + .WithOne(x => x.Exercise) + .HasForeignKey(x => x.ExerciseId); + modelBuilder.Entity() + .HasKey(x => x.Id); } } + diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs index c481cd17..ed2d0a0e 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs @@ -1,55 +1,55 @@ using ExerciseTracker.Study.Models; using ExerciseTracker.Study.Models.DTO; using ExerciseTracker.Study.Services; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -namespace ExerciseTracker.Study.Controllers +namespace ExerciseTracker.Study.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class ExerciseController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class ExerciseController : ControllerBase + private readonly IService Service; + public ExerciseController(IService service) + { + Service = service; + } + [HttpGet] + public async Task>> GetAllExercises() + { + return await Service.GetAll(); + } + [HttpGet] + [Route("{Id:int}")] + public async Task>> GetExerciseById([FromRoute] int Id) + { + return await Service.GetById(Id); + } + [HttpPost] + public async Task>> Create([FromBody] ExerciseDto NewExercise) { - private readonly IService Service; - public ExerciseController(IService service) - { - Service = service; - } - [HttpGet] - public async Task>> GetAllExercises() - { - return await Service.GetAll(); - } - [HttpGet] - [Route("{Id:int}")] - public async Task>> GetExerciseById([FromRoute] int Id) - { - return await Service.GetById(Id); - } - [HttpPost] - public async Task>> Create([FromBody] ExerciseDto NewExercise) - { - return await Service.Create(new Exercise { - Name=NewExercise.Name } - ); - } - [HttpPut] - [Route("{Id:int}")] - public async Task>> Update([FromRoute] int Id,[FromBody] ExerciseDto UpdateExerciseDto) + return await Service.Create(new Exercise { - Exercise UpdateExercise = new() - { - Id= Id, - Name=UpdateExerciseDto.Name - }; - return await Service.Update(UpdateExercise); + Name = NewExercise.Name } - [HttpDelete] - [Route("{Id:int}")] - public async Task>> Delete([FromRoute]int Id) + ); + } + [HttpPut] + [Route("{Id:int}")] + public async Task>> Update([FromRoute] int Id, [FromBody] ExerciseDto UpdateExerciseDto) + { + Exercise UpdateExercise = new() { - return await Service.Delete(Id); - } + Id = Id, + Name = UpdateExerciseDto.Name + }; + return await Service.Update(UpdateExercise); + } + [HttpDelete] + [Route("{Id:int}")] + public async Task>> Delete([FromRoute] int Id) + { + return await Service.Delete(Id); } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs index 9dae77ba..36bf2de5 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs @@ -4,69 +4,68 @@ using ExerciseTracker.Study.Services; using Microsoft.AspNetCore.Mvc; -namespace ExerciseTracker.Study.Controllers +namespace ExerciseTracker.Study.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class ExerciseShiftController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class ExerciseShiftController : ControllerBase + private readonly IService ShiftService; + public ExerciseShiftController(IService service) { - private readonly IService ShiftService; - public ExerciseShiftController(IService service) - { - ShiftService = service; - } - [HttpGet] - public async Task>> GetAllShifts() - { - return await ShiftService.GetAll(); - } - [HttpGet] - [Route("{Id:int}")] - public async Task>> GetById([FromRoute] int Id) - { - return await ShiftService.GetById(Id); - } - [HttpPost] - public async Task>> CreateShift([FromBody] ExerciseShiftDto NewExerciseDto) + ShiftService = service; + } + [HttpGet] + public async Task>> GetAllShifts() + { + return await ShiftService.GetAll(); + } + [HttpGet] + [Route("{Id:int}")] + public async Task>> GetById([FromRoute] int Id) + { + return await ShiftService.GetById(Id); + } + [HttpPost] + public async Task>> CreateShift([FromBody] ExerciseShiftDto NewExerciseDto) + { + ExerciseShift NewExerciseShift = HelperClass.ConvertToExercise(NewExerciseDto); + if (NewExerciseShift == null) { - ExerciseShift NewExerciseShift = HelperClass.ConvertToExercise(NewExerciseDto); - if (NewExerciseShift == null) + return new ResponseDto { - return new ResponseDto - { - IsSuccess = false, - Data = new List(), - Message = "Bad Data! please check if The Shift Date and Times are in correct format", - ResponseMethod = "Post" - }; + IsSuccess = false, + Data = new List(), + Message = "Bad Data! please check if The Shift Date and Times are in correct format", + ResponseMethod = "Post" + }; - } - return await ShiftService.Create(NewExerciseShift); } - [HttpPut] - [Route("{Id:int}")] - public async Task>> UpdateShift([FromRoute] int Id, [FromBody] ExerciseShiftDto UpdateExerciseDto) + return await ShiftService.Create(NewExerciseShift); + } + [HttpPut] + [Route("{Id:int}")] + public async Task>> UpdateShift([FromRoute] int Id, [FromBody] ExerciseShiftDto UpdateExerciseDto) + { + ExerciseShift UpdateExerciseShift = HelperClass.ConvertToExercise(UpdateExerciseDto); + if (UpdateExerciseShift == null) { - ExerciseShift UpdateExerciseShift = HelperClass.ConvertToExercise(UpdateExerciseDto); - if (UpdateExerciseShift == null) + return new ResponseDto { - return new ResponseDto - { - IsSuccess = false, - Data = new List(), - Message = "Bad Data! please check if The Shift Date and Times are in correct format", - ResponseMethod = "Post" - }; - } - UpdateExerciseShift.Id = Id; - return await ShiftService.Update(UpdateExerciseShift); + IsSuccess = false, + Data = new List(), + Message = "Bad Data! please check if The Shift Date and Times are in correct format", + ResponseMethod = "Post" + }; } - [HttpDelete] - [Route("{Id:int}")] - public async Task>> DeleteShift([FromRoute] int Id) - { + UpdateExerciseShift.Id = Id; + return await ShiftService.Update(UpdateExerciseShift); + } + [HttpDelete] + [Route("{Id:int}")] + public async Task>> DeleteShift([FromRoute] int Id) + { - return await ShiftService.Delete(Id); - } + return await ShiftService.Delete(Id); } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs index dd7ea716..21d711f9 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/DbSettings.cs @@ -1,7 +1,7 @@ -namespace ExerciseTracker.Study -{ +namespace ExerciseTracker.Study; + public class DbSettings { public string Default { get; set; } } -} + diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs index 60863cb6..4001405d 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs @@ -2,28 +2,27 @@ using ExerciseTracker.Study.Models.DTO; using ExerciseTracker.Study.ValidationsMethods; -namespace ExerciseTracker.HemanthSharma.HelperMethods +namespace ExerciseTracker.HemanthSharma.HelperMethods; + +public class HelperClass { - public class HelperClass + public static ExerciseShift ConvertToExercise(ExerciseShiftDto ExerciseDto) { - public static ExerciseShift ConvertToExercise(ExerciseShiftDto ExerciseDto) + if (!Validations.ValidTime(ExerciseDto.StartTime) || !Validations.ValidTime(ExerciseDto.EndTime) || !Validations.ValidDate(ExerciseDto.ExerciseDate)) { - if (!Validations.ValidTime(ExerciseDto.StartTime) || !Validations.ValidTime(ExerciseDto.EndTime) || !Validations.ValidDate(ExerciseDto.ExerciseDate)) - { - return null; - } - DateTime ShiftStartTime = DateTime.ParseExact(ExerciseDto.StartTime, "HH:mm", null); - DateTime ShiftEndTime = DateTime.ParseExact(ExerciseDto.EndTime, "HH:mm", null); - DateTime ShiftDate = DateTime.ParseExact(ExerciseDto.ExerciseDate, "dd-MM-yyyy", null); - ExerciseShift NewExercise = new ExerciseShift - { - ExerciseId = ExerciseDto.ExerciseId, - StartTime = ShiftStartTime, - EndTime = ShiftEndTime, - Comments = ExerciseDto.Comments, - ExerciseDate = ShiftDate - }; - return NewExercise; + return null; } + DateTime ShiftStartTime = DateTime.ParseExact(ExerciseDto.StartTime, "HH:mm", null); + DateTime ShiftEndTime = DateTime.ParseExact(ExerciseDto.EndTime, "HH:mm", null); + DateTime ShiftDate = DateTime.ParseExact(ExerciseDto.ExerciseDate, "dd-MM-yyyy", null); + ExerciseShift NewExercise = new ExerciseShift + { + ExerciseId = ExerciseDto.ExerciseId, + StartTime = ShiftStartTime, + EndTime = ShiftEndTime, + Comments = ExerciseDto.Comments, + ExerciseDate = ShiftDate + }; + return NewExercise; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs index 693736b4..278482ca 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Interfaces/IEntity.cs @@ -1,7 +1,6 @@ -namespace ExerciseTracker.HemanthSharma.Interfaces +namespace ExerciseTracker.HemanthSharma.Interfaces; + +public interface IEntity { - public interface IEntity - { - int Id { get; set; } - } + int Id { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs index b7597565..6128a050 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ExerciseDto.cs @@ -1,7 +1,6 @@ -namespace ExerciseTracker.Study.Models.DTO +namespace ExerciseTracker.Study.Models.DTO; + +public class ExerciseDto { - public class ExerciseDto - { - public string Name { get; set; } - } + public string Name { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs index 9a4ab8b3..cdd0c753 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/DTO/ResponseDto.cs @@ -1,20 +1,19 @@ -namespace ExerciseTracker.Study.Models.DTO +namespace ExerciseTracker.Study.Models.DTO; + +public class ResponseDto { - public class ResponseDto + public bool IsSuccess { get; set; } + public string ResponseMethod { get; set; } + public string Message { get; set; } + public List? Data { get; set; } + public ResponseDto CreateResponse(bool IsSuccess, string Message, string method, List? Data) { - public bool IsSuccess { get; set; } - public string ResponseMethod { get; set; } - public string Message { get; set; } - public List? Data { get; set; } - public ResponseDto CreateResponse(bool IsSuccess, string Message, string method, List? Data) + return new ResponseDto { - return new ResponseDto - { - IsSuccess = IsSuccess, - Message = Message, - ResponseMethod = method, - Data = Data - }; - } + IsSuccess = IsSuccess, + Message = Message, + ResponseMethod = method, + Data = Data + }; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs index 03062b7b..68f82845 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/Exercise.cs @@ -1,13 +1,12 @@ using ExerciseTracker.HemanthSharma.Interfaces; using System.Text.Json.Serialization; -namespace ExerciseTracker.Study.Models +namespace ExerciseTracker.Study.Models; + +public class Exercise : IEntity { - public class Exercise : IEntity - { - public int Id { get; set; } - public string Name { get; set; } - [JsonIgnore] - public List? ExerciseShifts { get; set; } - } + public int Id { get; set; } + public string Name { get; set; } + [JsonIgnore] + public List? ExerciseShifts { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs index af2dd76d..e02415fa 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Models/ExerciseShift.cs @@ -1,16 +1,15 @@ using ExerciseTracker.HemanthSharma.Interfaces; -namespace ExerciseTracker.Study.Models +namespace ExerciseTracker.Study.Models; + +public class ExerciseShift : IEntity { - public class ExerciseShift : IEntity - { - public int Id { get; set; } - public int ExerciseId { get; set; } - public DateTime StartTime { get; set; } - public DateTime EndTime { get; set; } - public DateTime ExerciseDate { get; set; } - public int Duration => (int)(EndTime - StartTime).TotalMinutes; - public string? Comments { get; set; } - public Exercise Exercise { get; set; } - } + public int Id { get; set; } + public int ExerciseId { get; set; } + public DateTime StartTime { get; set; } + public DateTime EndTime { get; set; } + public DateTime ExerciseDate { get; set; } + public int Duration => (int)(EndTime - StartTime).TotalMinutes; + public string? Comments { get; set; } + public Exercise Exercise { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs index 4c28d524..9e1708a0 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Program.cs @@ -5,46 +5,44 @@ using ExerciseTracker.Study.Services; using Microsoft.EntityFrameworkCore; -namespace ExerciseTracker.HemanthSharma +namespace ExerciseTracker.HemanthSharma; + +public class Program { - public class Program + public static void Main(string[] args) { - public static void Main(string[] args) - { - var builder = WebApplication.CreateBuilder(args); + var builder = WebApplication.CreateBuilder(args); - // Add services to the container. + // Add services to the container. - builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(); - builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Default"))); - builder.Services.AddScoped, RepositoryDapper>(); - // builder.Services.AddScoped, RepositoryClass>(); - builder.Services.AddScoped, RepositoryClass>(); - builder.Services.AddScoped, ServiceClass>(); - //builder.Services.AddScoped, ServiceClass>(); - builder.Services.AddScoped, ServiceClass>(); - builder.Services.Configure(builder.Configuration.GetSection("ConnectionStrings")); + builder.Services.AddControllers(); + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Default"))); + builder.Services.AddScoped, RepositoryDapper>(); + builder.Services.AddScoped, RepositoryClass>(); + builder.Services.AddScoped, ServiceClass>(); + builder.Services.AddScoped, ServiceClass>(); + builder.Services.Configure(builder.Configuration.GetSection("ConnectionStrings")); - var app = builder.Build(); + var app = builder.Build(); - // Configure the HTTP request pipeline. - if (app.Environment.IsDevelopment()) - { - app.UseSwagger(); - app.UseSwaggerUI(); - } + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } - app.UseHttpsRedirection(); + app.UseHttpsRedirection(); - app.UseAuthorization(); + app.UseAuthorization(); - app.MapControllers(); + app.MapControllers(); - app.Run(); - } + app.Run(); } } + diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs index c2a47c85..f8f2fda7 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/IRepository.cs @@ -1,13 +1,12 @@ using ExerciseTracker.Study.Models.DTO; -namespace ExerciseTracker.Study.Repositories +namespace ExerciseTracker.Study.Repositories; + +public interface IRepository { - public interface IRepository - { - Task> GetAll(); - Task> GetById(int Id); - Task> Update(T Entity); - Task> Delete(int Id); - Task> Create(T Entity); - } + Task> GetAll(); + Task> GetById(int Id); + Task> Update(T Entity); + Task> Delete(int Id); + Task> Create(T Entity); } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs index 3b1beae3..3442477f 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs @@ -3,181 +3,180 @@ using ExerciseTracker.Study.Models.DTO; using Microsoft.EntityFrameworkCore; -namespace ExerciseTracker.Study.Repositories +namespace ExerciseTracker.Study.Repositories; + +public class RepositoryClass : IRepository where T : class, IEntity { - public class RepositoryClass : IRepository where T : class, IEntity + private readonly ContextClass Context; + private readonly DbSet DbSet; + public RepositoryClass(ContextClass _context) + { + Context = _context; + DbSet = _context.Set(); + } + public async Task> Create(T Entity) { - private readonly ContextClass Context; - private readonly DbSet DbSet; - public RepositoryClass(ContextClass _context) + try { - Context = _context; - DbSet = _context.Set(); + var EntityResponse = await DbSet.AddAsync(Entity); + await Context.SaveChangesAsync(); + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "POST", + Message = "Successfully Created The Entity", + Data = new List { EntityResponse.Entity } + }; } - public async Task> Create(T Entity) + catch (Exception e) { - try + return new ResponseDto { - var EntityResponse = await DbSet.AddAsync(Entity); - await Context.SaveChangesAsync(); + IsSuccess = false, + ResponseMethod = "POST", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + } + + public async Task> Delete(int Id) + { + try + { + var Entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Id); + if (Entity != null) + { + DbSet.Remove(Entity); + Context.SaveChanges(); return new ResponseDto { IsSuccess = true, - ResponseMethod = "POST", - Message = "Successfully Created The Entity", - Data = new List { EntityResponse.Entity } + ResponseMethod = "DELETE", + Message = "Successfully Deleted The Entities", + Data = [Entity] }; } - catch (Exception e) + else { return new ResponseDto { IsSuccess = false, - ResponseMethod = "POST", - Message = "Error Occured: " + e.Message, + ResponseMethod = "DELETE", + Message = "No Entity Found to Delete", Data = [] }; } } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "DELETE", + Message = "Error Occured: " + e.Message, + Data = [] + }; + } + + } - public async Task> Delete(int Id) + public async Task> GetAll() + { + try { - try + var DataList = await DbSet.ToListAsync(); + return new ResponseDto { - var Entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Id); - if (Entity != null) - { - DbSet.Remove(Entity); - Context.SaveChanges(); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "DELETE", - Message = "Successfully Deleted The Entities", - Data = [Entity] - }; - } - else - { - return new ResponseDto - { - IsSuccess = false, - ResponseMethod = "DELETE", - Message = "No Entity Found to Delete", - Data = [] - }; - } - } - catch (Exception e) + IsSuccess = true, + ResponseMethod = "GET", + Message = "Successfully Fetched All The Entities", + Data = DataList + }; + } + catch (Exception e) + { + return new ResponseDto { - return new ResponseDto - { - IsSuccess = false, - ResponseMethod = "DELETE", - Message = "Error Occured: " + e.Message, - Data = [] - }; - } - + IsSuccess = false, + ResponseMethod = "GET", + Message = "Error Occured: " + e.Message, + Data = [] + }; } + } - public async Task> GetAll() + public async Task> GetById(int Id) + { + try { - try - { - var DataList = await DbSet.ToListAsync(); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "GET", - Message = "Successfully Fetched All The Entities", - Data = DataList - }; - } - catch (Exception e) + var DataEntity = await DbSet.FindAsync(Id); + if (DataEntity == null) { return new ResponseDto { IsSuccess = false, ResponseMethod = "GET", - Message = "Error Occured: " + e.Message, - Data = [] + Message = "No Entity Found for given ID", + Data = new List { DataEntity } }; } + return new ResponseDto + { + IsSuccess = true, + ResponseMethod = "GET", + Message = "Successfully Fetched The Entity With Id", + Data = new List { DataEntity } + }; + } + catch (Exception e) + { + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "GET", + Message = "Error Occured: " + e.Message, + Data = [] + }; } + } - public async Task> GetById(int Id) + public async Task> Update(T Entity) + { + try { - try + if (await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Entity.Id) != null) { - var DataEntity = await DbSet.FindAsync(Id); - if (DataEntity == null) - { - return new ResponseDto - { - IsSuccess = false, - ResponseMethod = "GET", - Message = "No Entity Found for given ID", - Data = new List { DataEntity } - }; - } + DbSet.Update(Entity); + Context.SaveChanges(); return new ResponseDto { IsSuccess = true, - ResponseMethod = "GET", - Message = "Successfully Fetched The Entity With Id", - Data = new List { DataEntity } + ResponseMethod = "PUT", + Message = "Successfully Updated The Entities", + Data = [Entity] }; } - catch (Exception e) + else { return new ResponseDto { IsSuccess = false, - ResponseMethod = "GET", - Message = "Error Occured: " + e.Message, + ResponseMethod = "PUT", + Message = "No Entity Found to Update", Data = [] }; } } - - public async Task> Update(T Entity) + catch (Exception e) { - try - { - if (await DbSet.AsNoTracking().FirstOrDefaultAsync(x => x.Id == Entity.Id) != null) - { - DbSet.Update(Entity); - Context.SaveChanges(); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "PUT", - Message = "Successfully Updated The Entities", - Data = [Entity] - }; - } - else - { - return new ResponseDto - { - IsSuccess = false, - ResponseMethod = "PUT", - Message = "No Entity Found to Update", - Data = [] - }; - } - } - catch (Exception e) + return new ResponseDto { - return new ResponseDto - { - IsSuccess = false, - ResponseMethod = "PUT", - Message = "Error Occured: " + e.Message, - Data = [] - }; - } + IsSuccess = false, + ResponseMethod = "PUT", + Message = "Error Occured: " + e.Message, + Data = [] + }; } } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs index b3529a9f..11225ad9 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs @@ -4,176 +4,174 @@ using Microsoft.Data.SqlClient; using Microsoft.Extensions.Options; using Spectre.Console; -using System.Data.Common; -namespace ExerciseTracker.Study.Repositories +namespace ExerciseTracker.Study.Repositories; + +public class RepositoryDapper : IRepository { - public class RepositoryDapper : IRepository + private string ConnectionString { get; set; } + private DbSettings options { get; set; } + public RepositoryDapper(IOptions _options) { - private string ConnectionString {get;set;} - private DbSettings options { get; set; } - public RepositoryDapper(IOptions _options) - { - options = _options.Value; - ConnectionString = options.Default; - } + options = _options.Value; + ConnectionString = options.Default; + } - public async Task> Create(Exercise Entity) + public async Task> Create(Exercise Entity) + { + AnsiConsole.MarkupLine($"[Yellow]Creating The Exercise[darkblue] {Entity.Name}[/] Through[/] [orange3]Dapper[/]"); + try { - AnsiConsole.MarkupLine($"[Yellow]Creating The Exercise[darkblue] {Entity.Name}[/] Through[/] [orange3]Dapper[/]"); - try + using (SqlConnection conn = new(ConnectionString)) { - using (SqlConnection conn = new(ConnectionString)) - { - string CreateQuery = "Insert Into Exercises([Name]) Values(@Name);"; - int RowsAffected=conn.Execute(CreateQuery, new { Name = Entity.Name }); - AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "POST", - Data = [Entity], - Message = "Successfully Created Entity using Dapper" - }; - } - } - catch(Exception e) - { - AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + string CreateQuery = "Insert Into Exercises([Name]) Values(@Name);"; + int RowsAffected = conn.Execute(CreateQuery, new { Name = Entity.Name }); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); return new ResponseDto { - IsSuccess = false, + IsSuccess = true, ResponseMethod = "POST", - Data = [], - Message = e.Message + Data = [Entity], + Message = "Successfully Created Entity using Dapper" }; } - - } - - public async Task> Delete(int Id) + catch (Exception e) { - AnsiConsole.MarkupLine($"[Yellow]Deleting The Exercise[darkblue] [/] Through[/] [orange3]Dapper[/]"); - try + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto { - using (SqlConnection conn = new(ConnectionString)) - { - string DeleteQuery = "Delete from Exercises where Id=@id ;"; - int RowsAffected = conn.Execute(DeleteQuery, new { id = Id }); - AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "DELETE", - Data = [], - Message = "Successfully Deleted Entity using Dapper" - }; - } - } - catch (Exception e) + IsSuccess = false, + ResponseMethod = "POST", + Data = [], + Message = e.Message + }; + } + + + } + + public async Task> Delete(int Id) + { + AnsiConsole.MarkupLine($"[Yellow]Deleting The Exercise[darkblue] [/] Through[/] [orange3]Dapper[/]"); + try + { + using (SqlConnection conn = new(ConnectionString)) { - AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + string DeleteQuery = "Delete from Exercises where Id=@id ;"; + int RowsAffected = conn.Execute(DeleteQuery, new { id = Id }); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); return new ResponseDto { - IsSuccess = false, + IsSuccess = true, ResponseMethod = "DELETE", Data = [], - Message = e.Message + Message = "Successfully Deleted Entity using Dapper" }; } } - - public async Task> GetAll() + catch (Exception e) { - try + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto { - using (SqlConnection conn = new(ConnectionString)) - { - string GetAllQuery = "Select * from Exercises ;"; - List Exercises= conn.Query(GetAllQuery).ToList(); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "GET", - Data = Exercises, - Message = "Successfully Retrieved All the Entities using Dapper" - }; - } - } - catch (Exception e) + IsSuccess = false, + ResponseMethod = "DELETE", + Data = [], + Message = e.Message + }; + } + } + + public async Task> GetAll() + { + try + { + using (SqlConnection conn = new(ConnectionString)) { - AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + string GetAllQuery = "Select * from Exercises ;"; + List Exercises = conn.Query(GetAllQuery).ToList(); return new ResponseDto { - IsSuccess = false, + IsSuccess = true, ResponseMethod = "GET", - Data = [], - Message = e.Message + Data = Exercises, + Message = "Successfully Retrieved All the Entities using Dapper" }; } } - - public async Task> GetById(int Id) + catch (Exception e) { - try + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto { - using (SqlConnection conn = new(ConnectionString)) - { - string GetQueryById = "Select * from Exercises where Id=@id ;"; - List Exercises = conn.Query(GetQueryById, new {id=Id}).ToList(); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "GET", - Data = Exercises, - Message = "Successfully Retrieved All the Entities using Dapper" - }; - } - } - catch (Exception e) + IsSuccess = false, + ResponseMethod = "GET", + Data = [], + Message = e.Message + }; + } + } + + public async Task> GetById(int Id) + { + try + { + using (SqlConnection conn = new(ConnectionString)) { - AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + string GetQueryById = "Select * from Exercises where Id=@id ;"; + List Exercises = conn.Query(GetQueryById, new { id = Id }).ToList(); return new ResponseDto { - IsSuccess = false, + IsSuccess = true, ResponseMethod = "GET", - Data = [], - Message = e.Message + Data = Exercises, + Message = "Successfully Retrieved All the Entities using Dapper" }; } } - - public async Task> Update(Exercise Entity) + catch (Exception e) { - try + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto { - using (SqlConnection conn = new(ConnectionString)) - { - string UpdateQuery = "Update Exercises" + - " set Name=@name where Id=@id;"; - int RowsAffected = conn.Execute(UpdateQuery, new { Name = Entity.Name, id=Entity.Id}); - AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); - return new ResponseDto - { - IsSuccess = true, - ResponseMethod = "PUT", - Data = [Entity], - Message = "Successfully Updated Entity using Dapper" - }; - } - } - catch (Exception e) + IsSuccess = false, + ResponseMethod = "GET", + Data = [], + Message = e.Message + }; + } + } + + public async Task> Update(Exercise Entity) + { + try + { + using (SqlConnection conn = new(ConnectionString)) { - AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + string UpdateQuery = "Update Exercises" + + " set Name=@name where Id=@id;"; + int RowsAffected = conn.Execute(UpdateQuery, new { Name = Entity.Name, id = Entity.Id }); + AnsiConsole.MarkupLine($"The Following Rows are Affected {RowsAffected}"); return new ResponseDto { - IsSuccess = false, + IsSuccess = true, ResponseMethod = "PUT", - Data = [], - Message = e.Message + Data = [Entity], + Message = "Successfully Updated Entity using Dapper" }; } } + catch (Exception e) + { + AnsiConsole.MarkupLine($"Something Wrong {e.Message}"); + return new ResponseDto + { + IsSuccess = false, + ResponseMethod = "PUT", + Data = [], + Message = e.Message + }; + } } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs index 68f6a323..281a8797 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/IService.cs @@ -1,13 +1,12 @@ using ExerciseTracker.Study.Models.DTO; -namespace ExerciseTracker.Study.Services +namespace ExerciseTracker.Study.Services; + +public interface IService { - public interface IService - { - Task> GetAll(); - Task> GetById(int Id); - Task> Update(T Entity); - Task> Delete(int Id); - Task> Create(T Entity); - } + Task> GetAll(); + Task> GetById(int Id); + Task> Update(T Entity); + Task> Delete(int Id); + Task> Create(T Entity); } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs index fc6b7533..108d33b7 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Services/ServiceClass.cs @@ -1,38 +1,37 @@ using ExerciseTracker.Study.Models.DTO; using ExerciseTracker.Study.Repositories; -namespace ExerciseTracker.Study.Services +namespace ExerciseTracker.Study.Services; + +public class ServiceClass : IService where T : class { - public class ServiceClass : IService where T:class + private readonly IRepository Repo; + public ServiceClass(IRepository _repo) + { + Repo = _repo; + } + public async Task> Create(T Entity) { - private readonly IRepository Repo; - public ServiceClass(IRepository _repo) - { - Repo = _repo; - } - public async Task> Create(T Entity) - { - return await Repo.Create(Entity); - } + return await Repo.Create(Entity); + } - public async Task> Delete(int Id) - { - return await Repo.Delete(Id); - } + public async Task> Delete(int Id) + { + return await Repo.Delete(Id); + } - public async Task> GetAll() - { - return await Repo.GetAll(); - } + public async Task> GetAll() + { + return await Repo.GetAll(); + } - public async Task> GetById(int Id) - { - return await Repo.GetById(Id); - } + public async Task> GetById(int Id) + { + return await Repo.GetById(Id); + } - public async Task> Update(T Entity) - { - return await Repo.Update(Entity); - } + public async Task> Update(T Entity) + { + return await Repo.Update(Entity); } } diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs index 856e8cd3..764b53af 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/ValidationsMethods/Validations.cs @@ -1,7 +1,7 @@ using System.Globalization; -namespace ExerciseTracker.Study.ValidationsMethods -{ +namespace ExerciseTracker.Study.ValidationsMethods; + public class Validations { public static bool ValidDate(string Datestring) @@ -13,4 +13,4 @@ public static bool ValidTime(string Timestring) return DateTime.TryParseExact(Timestring, "HH:mm", null, DateTimeStyles.None, out DateTime ShiftDate); } } -} + diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs index b68a066a..d19f760a 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HelperMethods.cs @@ -1,21 +1,15 @@ using ExerciseTracker.UI.Models; -using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +internal static class HelperMethods { - internal static class HelperMethods + public static ExerciseShiftDto RefineExerciseShift(ExerciseShiftDto Shift) { - public static ExerciseShiftDto RefineExerciseShift(ExerciseShiftDto Shift) - { - Shift.StartTime = (DateTime.ParseExact(Shift.StartTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); - Shift.EndTime = (DateTime.ParseExact(Shift.EndTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); - Shift.ExerciseDate = (DateTime.Parse(Shift.ExerciseDate)).ToString("dd-MM-yyyy"); - return Shift; - } + Shift.StartTime = (DateTime.ParseExact(Shift.StartTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); + Shift.EndTime = (DateTime.ParseExact(Shift.EndTime, "HH:mm", null, DateTimeStyles.None)).ToString("HH:mm"); + Shift.ExerciseDate = (DateTime.Parse(Shift.ExerciseDate)).ToString("dd-MM-yyyy"); + return Shift; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs index 4e118c0d..528aae86 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs @@ -1,22 +1,21 @@ using System.Configuration; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +public class HttpClientService { - public class HttpClientService + private string BaseUrl { get; set; } + public HttpClientService() + { + BaseUrl = ConfigurationManager.AppSettings["BaseUrl"]; + } + public string GetBaseURL() + { + return BaseUrl; + } + public HttpClient GetHttpClient() { - private string BaseUrl { get; set; } - public HttpClientService() - { - BaseUrl = ConfigurationManager.AppSettings["BaseUrl"]; - } - public string GetBaseURL() - { - return BaseUrl; - } - public HttpClient GetHttpClient() - { - HttpClient client = new HttpClient(); - return client; - } + HttpClient client = new HttpClient(); + return client; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs deleted file mode 100644 index 05bc383f..00000000 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IExercise.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ExerciseTracker.UI.Interfaces -{ - public interface IExercise - { - string name { get; set; } - } -} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs deleted file mode 100644 index 1011f83f..00000000 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Interfaces/IShift.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ExerciseTracker.UI.Interfaces -{ - public interface IShift - { - int Id { get; set; } - public DateTime StartTime { get; set; } - public DateTime EndTime { get; set; } - public DateTime ExerciseDate { get; set; } - } -} diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs index 6bfa8b94..dc3ea596 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/Exercise.cs @@ -1,17 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; +using System.Text.Json.Serialization; -namespace ExerciseTracker.UI.Models +namespace ExerciseTracker.UI.Models; + +public class Exercise { - public class Exercise - { - [property:JsonPropertyName("id")] - public int? id { get; set; } - [property: JsonPropertyName("name")] - public string name { get; set; } - } + [property: JsonPropertyName("id")] + public int? id { get; set; } + [property: JsonPropertyName("name")] + public string name { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs index c58e0519..bd1eaeba 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseDto.cs @@ -1,13 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace ExerciseTracker.UI.Models; -namespace ExerciseTracker.UI.Models +public class ExerciseDto { - public class ExerciseDto - { - public string Name { get; set; } - } + public string Name { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs index 304d88b0..c658c46e 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShift.cs @@ -1,28 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; +using System.Text.Json.Serialization; -namespace ExerciseTracker.UI.Models +namespace ExerciseTracker.UI.Models; + +public class ExerciseShift { - public class ExerciseShift - { - [property: JsonPropertyName("id")] - public int? Id { get; set; } - [property: JsonPropertyName("exerciseId")] - public int ExerciseId { get; set; } - [property: JsonPropertyName("startTime")] - public DateTime StartTime { get; set; } - [property: JsonPropertyName("endTime")] - public DateTime EndTime { get; set; } - [property: JsonPropertyName("exerciseDate")] - public DateTime ExerciseDate { get; set; } - [property: JsonPropertyName("duration")] - public int? Duration { get; set; } - [property: JsonPropertyName("comments")] - public string? Comments { get; set; } - - } + [property: JsonPropertyName("id")] + public int? Id { get; set; } + [property: JsonPropertyName("exerciseId")] + public int ExerciseId { get; set; } + [property: JsonPropertyName("startTime")] + public DateTime StartTime { get; set; } + [property: JsonPropertyName("endTime")] + public DateTime EndTime { get; set; } + [property: JsonPropertyName("exerciseDate")] + public DateTime ExerciseDate { get; set; } + [property: JsonPropertyName("duration")] + public int? Duration { get; set; } + [property: JsonPropertyName("comments")] + public string? Comments { get; set; } + } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs index a4aa9f98..9faf2a56 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ExerciseShiftDto.cs @@ -1,26 +1,19 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; +using System.Text.Json.Serialization; -namespace ExerciseTracker.UI.Models +namespace ExerciseTracker.UI.Models; + +public class ExerciseShiftDto { - public class ExerciseShiftDto - { - [property:JsonPropertyName("id")] - public int? Id { get; set; } - [property: JsonPropertyName("exerciseId")] - public int ExerciseId { get; set; } - [property: JsonPropertyName("startTime")] - public string StartTime { get; set; } - [property: JsonPropertyName("endTime")] - public string EndTime { get; set; } - [property: JsonPropertyName("exerciseDate")] - public string ExerciseDate { get; set; } - [property: JsonPropertyName("comments")] - public string? Comments { get; set; } - } + [property: JsonPropertyName("id")] + public int? Id { get; set; } + [property: JsonPropertyName("exerciseId")] + public int ExerciseId { get; set; } + [property: JsonPropertyName("startTime")] + public string StartTime { get; set; } + [property: JsonPropertyName("endTime")] + public string EndTime { get; set; } + [property: JsonPropertyName("exerciseDate")] + public string ExerciseDate { get; set; } + [property: JsonPropertyName("comments")] + public string? Comments { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs index 2332761b..8952e9af 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Models/ResponseDto.cs @@ -1,21 +1,15 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; +using System.Text.Json.Serialization; -namespace ExerciseTracker.UI.Models +namespace ExerciseTracker.UI.Models; + +public class ResponseDto where T : class { - public class ResponseDto where T: class - { - [property:JsonPropertyName("isSuccess")] - public bool IsSuccess { get; set; } - [property: JsonPropertyName("responseMethod")] - public string ResponseMethod { get; set; } - [property: JsonPropertyName("message")] - public string Message { get; set; } - [property: JsonPropertyName("data")] - public List? Data { get; set; } - } + [property: JsonPropertyName("isSuccess")] + public bool IsSuccess { get; set; } + [property: JsonPropertyName("responseMethod")] + public string ResponseMethod { get; set; } + [property: JsonPropertyName("message")] + public string Message { get; set; } + [property: JsonPropertyName("data")] + public List? Data { get; set; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs index 1c1bf640..5bf1d461 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Program.cs @@ -1,13 +1,12 @@ using ExerciseTracker.UI; -namespace Exercisetracker.UI +namespace Exercisetracker.UI; + +internal class Program { - internal class Program + static void Main(string[] args) { - static void Main(string[] args) - { - bool isRunning = true; - UserInterface.MainMenu(); - } + bool isRunning = true; + UserInterface.MainMenu(); } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs index 10970857..8b5e5c5a 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs @@ -1,149 +1,143 @@ using ExerciseTracker.UI.Models; -using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http.Json; -using System.Text; using System.Text.Json; -using System.Threading.Tasks; -namespace ExerciseTracker.UI.Repositories +namespace ExerciseTracker.UI.Repositories; + +public class Repository where T : class { - public class Repository where T : class + public string BaseURL { get; set; } + HttpClientService ClientService = new(); + HttpClient Client; + public Repository() { - public string BaseURL { get; set; } - HttpClientService ClientService = new(); - HttpClient Client; - public Repository() + if (typeof(T).Name == "ExerciseShiftDto") { - if (typeof(T).Name == "ExerciseShiftDto") - { - BaseURL = ClientService.GetBaseURL() + "ExerciseShift"; - } - else - { - BaseURL = ClientService.GetBaseURL() + typeof(T).Name; - } - Client = ClientService.GetHttpClient(); + BaseURL = ClientService.GetBaseURL() + "ExerciseShift"; } - public async Task> GetAllEntities() + else + { + BaseURL = ClientService.GetBaseURL() + typeof(T).Name; + } + Client = ClientService.GetHttpClient(); + } + public async Task> GetAllEntities() + { + try { - try + //https://localhost:7249/api/Exercise + //https://localhost:7249/api/ExerciseShift + string Stringresponse = await Client.GetStringAsync(BaseURL); + using (var response = await Client.GetStreamAsync(BaseURL)) { - //https://localhost:7249/api/Exercise - //https://localhost:7249/api/ExerciseShift - string Stringresponse = await Client.GetStringAsync(BaseURL); - using (var response = await Client.GetStreamAsync(BaseURL)) - { - ResponseDto GetResponse = JsonSerializer.Deserialize>(response); - return GetResponse; - } + ResponseDto GetResponse = JsonSerializer.Deserialize>(response); + return GetResponse; } - catch(Exception e) + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto { - Console.WriteLine(e.Message); - return new ResponseDto - { - IsSuccess=false, - Message=e.Message, - ResponseMethod="GET", - Data = [] - }; - } + IsSuccess = false, + Message = e.Message, + ResponseMethod = "GET", + Data = [] + }; } - public async Task> GetEntiryById(int? Id) + } + public async Task> GetEntiryById(int? Id) + { + try { - try + using (var response = await Client.GetStreamAsync(BaseURL + $"/{Id}")) { - using (var response = await Client.GetStreamAsync(BaseURL + $"/{Id}")) - { - ResponseDto GetResponse = JsonSerializer.Deserialize>(response); - return GetResponse; - } + ResponseDto GetResponse = JsonSerializer.Deserialize>(response); + return GetResponse; } - catch (Exception e) + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto { - Console.WriteLine(e.Message); - return new ResponseDto - { - IsSuccess = false, - Message = e.Message, - ResponseMethod = "GET", - Data = [] - }; - } + IsSuccess = false, + Message = e.Message, + ResponseMethod = "GET", + Data = [] + }; } - public async Task> CreateEntity(T Entity) + } + public async Task> CreateEntity(T Entity) + { + try { - try + var response = await Client.PostAsJsonAsync(BaseURL, Entity); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { - var response = await Client.PostAsJsonAsync(BaseURL, Entity); - using (var StreamResponse = await response.Content.ReadAsStreamAsync()) - { - ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); - return Response; - } - + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; } - catch (Exception e) + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto { - Console.WriteLine(e.Message); - return new ResponseDto - { - IsSuccess = false, - Message = e.Message, - ResponseMethod = "POST", - Data = [] - }; - } + IsSuccess = false, + Message = e.Message, + ResponseMethod = "POST", + Data = [] + }; } - - public async Task> UpdateEntity(T Entity, int? Id) + } + + public async Task> UpdateEntity(T Entity, int? Id) + { + try { - try + var response = await Client.PutAsJsonAsync(BaseURL + $"/{Id}", Entity); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { - var response = await Client.PutAsJsonAsync(BaseURL + $"/{Id}", Entity); - using (var StreamResponse = await response.Content.ReadAsStreamAsync()) - { - ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); - return Response; - } + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; } - catch (Exception e) + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto { - Console.WriteLine(e.Message); - return new ResponseDto - { - IsSuccess = false, - Message = e.Message, - ResponseMethod = "PUT", - Data = [] - }; - } + IsSuccess = false, + Message = e.Message, + ResponseMethod = "PUT", + Data = [] + }; } - public async Task> DeleteEntity(int? Id) + } + public async Task> DeleteEntity(int? Id) + { + try { - try + var response = await Client.DeleteAsync(BaseURL + $"/{Id}"); + using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { - var response = await Client.DeleteAsync(BaseURL + $"/{Id}"); - using (var StreamResponse = await response.Content.ReadAsStreamAsync()) - { - ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); - return Response; - } + ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); + return Response; } - catch (Exception e) + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return new ResponseDto { - Console.WriteLine(e.Message); - return new ResponseDto - { - IsSuccess = false, - Message = e.Message, - ResponseMethod = "DELETE", - Data = [] - }; - } + IsSuccess = false, + Message = e.Message, + ResponseMethod = "DELETE", + Data = [] + }; } - } + } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs index 6685aa5c..6ab7af54 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs @@ -1,53 +1,47 @@ using ExerciseTracker.UI.Models; using ExerciseTracker.UI.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ExerciseTracker.UI.Services +namespace ExerciseTracker.UI.Services; + +public class ExerciseService { - public class ExerciseService + private static readonly Repository Repo = new Repository(); + public static void CreateExercise() { - private static readonly Repository Repo= new Repository(); - public static void CreateExercise() - { - Exercise NewExercise = UserInputs.GetNewExercise(); - ResponseDto CreatedExercise= Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(CreatedExercise); - } + Exercise NewExercise = UserInputs.GetNewExercise(); + ResponseDto CreatedExercise = Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(CreatedExercise); + } - public static void DeleteExercise() - { - List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; - int? UserChoice = UserInputs.GetExerciseById(Exercises); - ResponseDto DeleteResponse = Repo.DeleteEntity(UserChoice).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(DeleteResponse); - } + public static void DeleteExercise() + { + List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetExerciseById(Exercises); + ResponseDto DeleteResponse = Repo.DeleteEntity(UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(DeleteResponse); + } - public static void GetAllExercises() - { - ResponseDto Response = Repo.GetAllEntities().GetAwaiter().GetResult(); - UserOutputs.ShowResponse(Response); - } + public static void GetAllExercises() + { + ResponseDto Response = Repo.GetAllEntities().GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); + } - public static void GetSingleExercise() - { - List Exercises= Repo.GetAllEntities().GetAwaiter().GetResult().Data; - int? UserChoice=UserInputs.GetExerciseById(Exercises); - ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(Response); - } + public static void GetSingleExercise() + { + List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetExerciseById(Exercises); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); + } - public static void UpdateExercise() - { - List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; - int? UserChoice = UserInputs.GetExerciseById(Exercises); - ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); - Exercise UpdatedExercise = UserInputs.GetUpdatedEntity(Response.Data); - ResponseDto DeleteResponse = Repo.UpdateEntity(UpdatedExercise, UserChoice).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(Response); - } + public static void UpdateExercise() + { + List Exercises = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetExerciseById(Exercises); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + Exercise UpdatedExercise = UserInputs.GetUpdatedEntity(Response.Data); + ResponseDto DeleteResponse = Repo.UpdateEntity(UpdatedExercise, UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(Response); } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs index f2653824..56f5e296 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ShiftService.cs @@ -1,70 +1,64 @@ using ExerciseTracker.UI.Models; using ExerciseTracker.UI.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ExerciseTracker.UI.Services +namespace ExerciseTracker.UI.Services; + +public static class ShiftService { - public static class ShiftService + public static readonly Repository Repo = new Repository(); + public static readonly Repository ExerciseRepo = new Repository(); + public static List GetAvailableExercises() { - public static readonly Repository Repo = new Repository(); - public static readonly Repository ExerciseRepo = new Repository(); - public static List GetAvailableExercises() - { - ResponseDto ExercisesResponse = ExerciseRepo.GetAllEntities().GetAwaiter().GetResult(); - return ExercisesResponse.Data; - } - - public static void CreateShift() - { - List AvailableExercises = GetAvailableExercises(); - ExerciseShiftDto NewExercise = UserInputs.GetNewShift(AvailableExercises); - ResponseDto CreateResponse=Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(CreateResponse); - } + ResponseDto ExercisesResponse = ExerciseRepo.GetAllEntities().GetAwaiter().GetResult(); + return ExercisesResponse.Data; + } - public static void DeleteShift() + public static void CreateShift() + { + List AvailableExercises = GetAvailableExercises(); + ExerciseShiftDto NewExercise = UserInputs.GetNewShift(AvailableExercises); + ResponseDto CreateResponse = Repo.CreateEntity(NewExercise).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(CreateResponse); + } + + public static void DeleteShift() + { + ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); + int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); + if (SelectedShiftId == -1) { - ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); - int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); - if(SelectedShiftId==-1) - { - return; - } - ResponseDto DeleteShiftRsponse = Repo.DeleteEntity(SelectedShiftId).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(DeleteShiftRsponse); + return; } + ResponseDto DeleteShiftRsponse = Repo.DeleteEntity(SelectedShiftId).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(DeleteShiftRsponse); + } - public static void GetAllShifts() - { - ResponseDto ShiftList=Repo.GetAllEntities().GetAwaiter().GetResult(); - UserOutputs.ShowResponse(ShiftList); - } + public static void GetAllShifts() + { + ResponseDto ShiftList = Repo.GetAllEntities().GetAwaiter().GetResult(); + UserOutputs.ShowResponse(ShiftList); + } - public static void GetSingleShift() + public static void GetSingleShift() + { + ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); + int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); + if (SelectedShiftId == -1) { - ResponseDto ShiftsList = Repo.GetAllEntities().GetAwaiter().GetResult(); - int SelectedShiftId = UserInputs.GetShiftById(ShiftsList.Data); - if(SelectedShiftId==-1) - { - return; - } - ResponseDto ShiftByIdRsponse=Repo.GetEntiryById(SelectedShiftId).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(ShiftByIdRsponse); + return; } + ResponseDto ShiftByIdRsponse = Repo.GetEntiryById(SelectedShiftId).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(ShiftByIdRsponse); + } - public static void UpdateShift() - { - List ExerciseShifts = Repo.GetAllEntities().GetAwaiter().GetResult().Data; - int? UserChoice = UserInputs.GetShiftById(ExerciseShifts); - ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); - ExerciseShiftDto UpdatedExerciseShift = UserInputs.GetUpdatedEntity(Response.Data); - UpdatedExerciseShift = HelperMethods.RefineExerciseShift(UpdatedExerciseShift); - ResponseDto UpdateResponse = Repo.UpdateEntity(UpdatedExerciseShift, UserChoice).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(UpdateResponse); - } + public static void UpdateShift() + { + List ExerciseShifts = Repo.GetAllEntities().GetAwaiter().GetResult().Data; + int? UserChoice = UserInputs.GetShiftById(ExerciseShifts); + ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); + ExerciseShiftDto UpdatedExerciseShift = UserInputs.GetUpdatedEntity(Response.Data); + UpdatedExerciseShift = HelperMethods.RefineExerciseShift(UpdatedExerciseShift); + ResponseDto UpdateResponse = Repo.UpdateEntity(UpdatedExerciseShift, UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(UpdateResponse); } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs index e9e18e5a..36b68dfe 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs @@ -2,176 +2,165 @@ using ExerciseTracker.UI.Repositories; using Spectre.Console; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +public class UserInputs where T : class { - public class UserInputs where T : class + internal static int? GetExerciseById(List Entities) { - internal static int? GetExerciseById(List Entities) - { - List UserChoices = new(); + List UserChoices = new(); - UserChoices = Entities.Select(x => x.name).ToList(); + UserChoices = Entities.Select(x => x.name).ToList(); - var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") - .AddChoices(UserChoices)); - return Entities.Where(x => x.name == UserOption).Select(x => x.id).FirstOrDefault(); - } - internal static int GetShiftById(List Entities) + var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") + .AddChoices(UserChoices)); + return Entities.Where(x => x.name == UserOption).Select(x => x.id).FirstOrDefault(); + } + internal static int GetShiftById(List Entities) + { + if (Entities.Count() == 0) { - if (Entities.Count() == 0) - { - AnsiConsole.MarkupLine("[pink3] No Entities Found!!![/]"); - Console.WriteLine("Click any key to Continue"); - Console.ReadLine(); - return -1; - } - List UserChoices = new(); - UserChoices = Entities.Select(x => $"ShiftId={x.Id};ShiftDate={x.ExerciseDate};ShiftStartTime={x.StartTime}").ToList(); - var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") - .AddChoices(UserChoices)); - return int.Parse(UserOption.Split(";")[0].Split("=")[1]); + AnsiConsole.MarkupLine("[pink3] No Entities Found!!![/]"); + Console.WriteLine("Click any key to Continue"); + Console.ReadLine(); + return -1; } + List UserChoices = new(); + UserChoices = Entities.Select(x => $"ShiftId={x.Id};ShiftDate={x.ExerciseDate};ShiftStartTime={x.StartTime}").ToList(); + var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") + .AddChoices(UserChoices)); + return int.Parse(UserOption.Split(";")[0].Split("=")[1]); + } - internal static Exercise GetNewExercise() + internal static Exercise GetNewExercise() + { + string Name = AnsiConsole.Ask("[yellow]Enter the [Blue]Name[/] of the Exercise you want to Add:[/]"); + return new Exercise { - string Name = AnsiConsole.Ask("[yellow]Enter the [Blue]Name[/] of the Exercise you want to Add:[/]"); - return new Exercise - { - name = Name - }; - } + name = Name + }; + } - internal static T GetUpdatedEntity(List UpdatedList) + internal static T GetUpdatedEntity(List UpdatedList) + { + T UpdatedEntity = Activator.CreateInstance(); + var props = typeof(T).GetProperties(); + string UpdatedValue = ""; + foreach (var prop in props) { - T UpdatedEntity = Activator.CreateInstance(); - var props = typeof(T).GetProperties(); - string UpdatedValue = ""; - foreach (var prop in props) + string PropertyName = prop.Name; + if (PropertyName.ToLower() != "id") { - string PropertyName = prop.Name; - if (PropertyName.ToLower() != "id") + var res = AnsiConsole.Confirm($"[fuchsia]Do you want to change the[yellow] {prop.Name}[/] Property:? The Current Value is [aqua]{prop.GetValue(UpdatedList[0])}[/][/]"); + if (res) { - var res = AnsiConsole.Confirm($"[fuchsia]Do you want to change the[yellow] {prop.Name.ToString()}[/] Property:? The Current Value is [aqua]{prop.GetValue(UpdatedList[0])}[/][/]"); - if (res) + if (PropertyName.ToLower() == "exerciseid") { - if (PropertyName.ToLower() == "exerciseid") - { - Repository Repo = new(); + Repository Repo = new(); - int? ExerciseId = UserInputs.GetExerciseById(Repo.GetAllEntities().GetAwaiter().GetResult().Data); - UpdatedValue = ExerciseId.ToString(); - - } - else if (PropertyName.ToLower() == "starttime") - { - UpdatedValue = UserInputs.GetShiftTime().ToString("HH:mm"); - } - else if (PropertyName.ToLower() == "endtime") - { - UpdatedValue = UserInputs.GetShiftTime(DateTime.Parse(UpdatedValue)).ToString("HH:mm"); - } - else if (PropertyName.ToLower() == "exercisedate") - { - UpdatedValue = UserInputs.GetShiftDate().ToString("dd-MM-yyyy"); - } - else - { - AnsiConsole.MarkupLine("[olive] Enter the Updated Value:[/]"); - UpdatedValue = Console.ReadLine(); - } + int? ExerciseId = UserInputs.GetExerciseById(Repo.GetAllEntities().GetAwaiter().GetResult().Data); + UpdatedValue = ExerciseId.ToString(); } - else + else if (PropertyName.ToLower() == "starttime") + { + UpdatedValue = UserInputs.GetShiftTime().ToString("HH:mm"); + } + else if (PropertyName.ToLower() == "endtime") { - UpdatedValue = prop.GetValue(UpdatedList[0]).ToString(); + UpdatedValue = UserInputs.GetShiftTime(DateTime.Parse(UpdatedValue)).ToString("HH:mm"); } - if (prop.Name.ToLower() == "exerciseid") + else if (PropertyName.ToLower() == "exercisedate") { - prop.SetValue(UpdatedEntity, int.Parse(UpdatedValue)); + UpdatedValue = UserInputs.GetShiftDate().ToString("dd-MM-yyyy"); } else { - prop.SetValue(UpdatedEntity, UpdatedValue); + AnsiConsole.MarkupLine("[olive] Enter the Updated Value:[/]"); + UpdatedValue = Console.ReadLine(); } + + } + else + { + UpdatedValue = prop.GetValue(UpdatedList[0]).ToString(); + } + if (prop.Name.ToLower() == "exerciseid") + { + prop.SetValue(UpdatedEntity, int.Parse(UpdatedValue)); + } + else + { + prop.SetValue(UpdatedEntity, UpdatedValue); } } - return UpdatedEntity; } + return UpdatedEntity; + } - internal static ExerciseShiftDto GetNewShift(List Exercises) + internal static ExerciseShiftDto GetNewShift(List Exercises) + { + AnsiConsole.MarkupLine("[cyan1]Choose the Exercise for which Shift is to be Created:[/]"); + List ExerciseNames = Exercises.Select(x => $"Id:{x.id} ExerciseName:{x.name}").ToList(); + string Userchoice = AnsiConsole.Prompt(new SelectionPrompt() + .Title("Please select an Exercise for the Shift:") + .AddChoices(ExerciseNames)); + int ExerciseId = int.Parse(Userchoice.Split(" ")[0].Split(":")[1]); + AnsiConsole.MarkupLine("[yellow3] Enter startTime of Shift[/]"); + DateTime StartTime = GetShiftTime(); + AnsiConsole.MarkupLine("[yellow3] Enter EndTime of Shift[/]"); + DateTime EndTime = GetShiftTime(StartTime); + DateTime ExerciseDate = GetShiftDate(); + AnsiConsole.MarkupLine("comments for this shift?"); + string? Comments = Console.ReadLine(); + return new ExerciseShiftDto { - AnsiConsole.MarkupLine("[cyan1]Choose the Exercise for which Shift is to be Created:[/]"); - //Table Responsetable = new Table(); - //Responsetable.Title = new TableTitle("[lightseagreen]Available Exercises[/]"); - //Responsetable.AddColumn("Id"); - //Responsetable.AddColumn("Name"); - //foreach (var Exercise in Exercises) - //{ - // Responsetable.AddRow(Exercise.id.ToString(),Exercise.name.ToString()); - //} - //Responsetable.Border = TableBorder.Double; - //AnsiConsole.Write(Responsetable); - List ExerciseNames = Exercises.Select(x => $"Id:{x.id} ExerciseName:{x.name}").ToList(); - string Userchoice = AnsiConsole.Prompt(new SelectionPrompt() - .Title("Please select an Exercise for the Shift:") - .AddChoices(ExerciseNames)); - int ExerciseId = int.Parse(Userchoice.Split(" ")[0].Split(":")[1]); - AnsiConsole.MarkupLine("[yellow3] Enter startTime of Shift[/]"); - DateTime StartTime = GetShiftTime(); - AnsiConsole.MarkupLine("[yellow3] Enter EndTime of Shift[/]"); - DateTime EndTime = GetShiftTime(StartTime); - DateTime ExerciseDate = GetShiftDate(); - AnsiConsole.MarkupLine("comments for this shift?"); - string? Comments = Console.ReadLine(); - return new ExerciseShiftDto - { - ExerciseId = ExerciseId, - StartTime = StartTime.ToString("HH:mm"), - EndTime = EndTime.ToString("HH:mm"), - ExerciseDate = ExerciseDate.ToString("dd-MM-yyyy"), - Comments = Comments - }; - } + ExerciseId = ExerciseId, + StartTime = StartTime.ToString("HH:mm"), + EndTime = EndTime.ToString("HH:mm"), + ExerciseDate = ExerciseDate.ToString("dd-MM-yyyy"), + Comments = Comments + }; + } - private static DateTime GetShiftDate() + private static DateTime GetShiftDate() + { + bool res = AnsiConsole.Confirm("[orange4] Do you want to Enter Custom date?[/][chartreuse2](Default value will be Today's Date)[/]"); + DateTime ExerciseDate; + if (res) { - bool res = AnsiConsole.Confirm("[orange4] Do you want to Enter Custom date?[/][chartreuse2](Default value will be Today's Date)[/]"); - DateTime ExerciseDate; - if (res) - { - ExerciseDate = Validations.GetValidDate(); - } - else - { - ExerciseDate = DateTime.Now.Date; - } - return ExerciseDate; + ExerciseDate = Validations.GetValidDate(); + } + else + { + ExerciseDate = DateTime.Now.Date; } + return ExerciseDate; + } - private static DateTime GetShiftTime(DateTime? StartTime = null) + private static DateTime GetShiftTime(DateTime? StartTime = null) + { + DateTime StartTimeValue = DateTime.Now; + bool isTimeValid = true; + if (StartTime != null) { - DateTime StartTimeValue = DateTime.Now; - bool isTimeValid = true; - if (StartTime != null) - { - StartTimeValue = (DateTime)StartTime; - isTimeValid = false; + StartTimeValue = (DateTime)StartTime; + isTimeValid = false; + } + DateTime TimeResult = Validations.GetValidTime(); + while (!isTimeValid) + { + if (TimeResult >= StartTime) + { + isTimeValid = true; } - DateTime TimeResult = Validations.GetValidTime(); - while (!isTimeValid) + else { - if (TimeResult >= StartTime) - { - isTimeValid = true; - } - else - { - AnsiConsole.MarkupLine($"[lightgreen]The StartTime of the Shift is {StartTimeValue.ToShortTimeString()} EndDate Can't be before StartTime [/]"); - TimeResult = Validations.GetValidTime(); - } + AnsiConsole.MarkupLine($"[lightgreen]The StartTime of the Shift is {StartTimeValue.ToShortTimeString()} EndDate Can't be before StartTime [/]"); + TimeResult = Validations.GetValidTime(); } - return TimeResult; } + return TimeResult; } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs index 51c29eb3..f7955c40 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInterface.cs @@ -1,111 +1,105 @@ using ExerciseTracker.UI.Services; using Spectre.Console; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +public class UserInterface { - public class UserInterface + public static void MainMenu() { - public static void MainMenu() + bool IsAppRunning = true; + while (IsAppRunning) { - bool IsAppRunning = true; - while (IsAppRunning) - { - Console.Clear(); - var userOption = AnsiConsole.Prompt( - new SelectionPrompt() - .Title("Please select an option") - .AddChoices(["Manage Shifts", "Manage Exercises", "Exit"]) - ); + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["Manage Shifts", "Manage Exercises", "Exit"]) + ); - switch (userOption) - { - case "Manage Shifts": - ShiftServiceMenu(); - break; - case "Manage Exercises": - ExerciseServiceMenu(); - break; - case "Exit": - IsAppRunning = false; - break; - } + switch (userOption) + { + case "Manage Shifts": + ShiftServiceMenu(); + break; + case "Manage Exercises": + ExerciseServiceMenu(); + break; + case "Exit": + IsAppRunning = false; + break; } } + } - private static void ExerciseServiceMenu() + private static void ExerciseServiceMenu() + { + bool isAppRunning = true; + while (isAppRunning) { - bool isAppRunning = true; - while (isAppRunning) - { - Console.Clear(); - var userOption = AnsiConsole.Prompt( - new SelectionPrompt() - .Title("Please select an option") - .AddChoices(["ViewAllExercises", "View a single exercise", "Delete a Exercise", "Create a new Exercise", "Update a Exercise", "Exit"]) - ); + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["ViewAllExercises", "View a single exercise", "Delete a Exercise", "Create a new Exercise", "Update a Exercise", "Exit"]) + ); - switch (userOption) - { - case "ViewAllExercises": - ExerciseService.GetAllExercises(); - break; - case "View a single exercise": - ExerciseService.GetSingleExercise(); - break; - case "Delete a Exercise": - ExerciseService.DeleteExercise(); - break; - case "Create a new Exercise": - ExerciseService.CreateExercise(); - break; - case "Update a Exercise": - ExerciseService.UpdateExercise(); - break; - case "Exit": - isAppRunning = false; - break; - } + switch (userOption) + { + case "ViewAllExercises": + ExerciseService.GetAllExercises(); + break; + case "View a single exercise": + ExerciseService.GetSingleExercise(); + break; + case "Delete a Exercise": + ExerciseService.DeleteExercise(); + break; + case "Create a new Exercise": + ExerciseService.CreateExercise(); + break; + case "Update a Exercise": + ExerciseService.UpdateExercise(); + break; + case "Exit": + isAppRunning = false; + break; } } + } - private static void ShiftServiceMenu() + private static void ShiftServiceMenu() + { + bool isAppRunning = true; + while (isAppRunning) { - bool isAppRunning = true; - while (isAppRunning) - { - Console.Clear(); - var userOption = AnsiConsole.Prompt( - new SelectionPrompt() - .Title("Please select an option") - .AddChoices(["ViewAllShifts", "View a single Shift", "Delete a shift", "Create a new Shift", "Update a Shift", "Exit"]) - ); + Console.Clear(); + var userOption = AnsiConsole.Prompt( + new SelectionPrompt() + .Title("Please select an option") + .AddChoices(["ViewAllShifts", "View a single Shift", "Delete a shift", "Create a new Shift", "Update a Shift", "Exit"]) + ); - switch (userOption) - { - case "ViewAllShifts": - ShiftService.GetAllShifts(); - break; - case "View a single Shift": - ShiftService.GetSingleShift(); - break; - case "Delete a shift": - ShiftService.DeleteShift(); - break; - case "Create a new Shift": - ShiftService.CreateShift(); - break; - case "Update a Shift": - ShiftService.UpdateShift(); - break; - case "Exit": - isAppRunning = false; - break; - } + switch (userOption) + { + case "ViewAllShifts": + ShiftService.GetAllShifts(); + break; + case "View a single Shift": + ShiftService.GetSingleShift(); + break; + case "Delete a shift": + ShiftService.DeleteShift(); + break; + case "Create a new Shift": + ShiftService.CreateShift(); + break; + case "Update a Shift": + ShiftService.UpdateShift(); + break; + case "Exit": + isAppRunning = false; + break; } } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs index 7dde1788..b23f995c 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs @@ -1,67 +1,66 @@ using ExerciseTracker.UI.Models; using Spectre.Console; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +public static class UserOutputs where T : class { - public static class UserOutputs where T : class + public static void ShowResponse(ResponseDto Response) { - public static void ShowResponse(ResponseDto Response) + Console.Clear(); + if (!Response.IsSuccess) { - Console.Clear(); - if (!Response.IsSuccess) + string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[maroon]Reponse Message:{Response.Message}[/][/]"; + var panel = new Panel(ResponseString); + panel.Header = new PanelHeader("[Red]Request Failed!!![/]"); + panel.Border = BoxBorder.Rounded; + panel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(panel); + Console.ReadLine(); + } + else + { + string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[green]Reponse Message:{Response.Message}[/][/]"; + var panel = new Panel(ResponseString); + panel.Header = new PanelHeader("[lime]Request Success!!![/]"); + panel.Border = BoxBorder.Rounded; + panel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(panel); + string Heading = Response.ResponseMethod switch + { + "GET" => "Here is the Entity Details", + "POST" => "Details of the Entity Created", + "PUT" => "Details of the updated Entity", + "DELETE" => "Details of the Entity Deleted", + _ => "Unknown" + }; + if (Response.Data.Count() == 0) { - string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[maroon]Reponse Message:{Response.Message}[/][/]"; - var panel = new Panel(ResponseString); - panel.Header = new PanelHeader("[Red]Request Failed!!![/]"); - panel.Border = BoxBorder.Rounded; - panel.Padding = new Padding(2, 2, 2, 2); - AnsiConsole.Write(panel); + ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\nCurrently No Data Found For the requested Entityy in DataBase[/]"; + var EmptyMessagePanel = new Panel(ResponseString); + EmptyMessagePanel.Header = new PanelHeader("[lime]Empty Data!!![/]"); + EmptyMessagePanel.Border = BoxBorder.Rounded; + EmptyMessagePanel.Padding = new Padding(2, 2, 2, 2); + AnsiConsole.Write(EmptyMessagePanel); Console.ReadLine(); } else { - string ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\n[green]Reponse Message:{Response.Message}[/][/]"; - var panel = new Panel(ResponseString); - panel.Header = new PanelHeader("[lime]Request Success!!![/]"); - panel.Border = BoxBorder.Rounded; - panel.Padding = new Padding(2, 2, 2, 2); - AnsiConsole.Write(panel); - string Heading = Response.ResponseMethod switch + AnsiConsole.MarkupLine(Heading); + Table Responsetable = new Table(); + Responsetable.Title = new TableTitle(typeof(T).Name); + var props = typeof(T).GetProperties().ToList(); + props.ForEach(x => Responsetable.AddColumn(Markup.Escape(x.Name))); + foreach (var ResponseObject in Response.Data) { - "GET" => "Here is the Entity Details", - "POST" => "Details of the Entity Created", - "PUT" => "Details of the updated Entity", - "DELETE" => "Details of the Entity Deleted", - _ => "Unknown" - }; - if (Response.Data.Count() == 0) - { - ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\nCurrently No Data Found For the requested Entityy in DataBase[/]"; - var EmptyMessagePanel = new Panel(ResponseString); - EmptyMessagePanel.Header = new PanelHeader("[lime]Empty Data!!![/]"); - EmptyMessagePanel.Border = BoxBorder.Rounded; - EmptyMessagePanel.Padding = new Padding(2, 2, 2, 2); - AnsiConsole.Write(EmptyMessagePanel); - Console.ReadLine(); - } - else - { - AnsiConsole.MarkupLine(Heading); - Table Responsetable = new Table(); - Responsetable.Title = new TableTitle(typeof(T).Name); - var props = typeof(T).GetProperties().ToList(); ; - props.ForEach(x => Responsetable.AddColumn(Markup.Escape(x.Name))); - foreach (var ResponseObject in Response.Data) - { - List RowData = new(); - props.ForEach(x => RowData.Add(x.GetValue(ResponseObject).ToString())); - Responsetable.AddRow(RowData.ToArray()); - } - Responsetable.Border = TableBorder.Double; - AnsiConsole.Write(Responsetable); + List RowData = new(); + props.ForEach(x => RowData.Add(x.GetValue(ResponseObject).ToString())); + Responsetable.AddRow(RowData.ToArray()); } + Responsetable.Border = TableBorder.Double; + AnsiConsole.Write(Responsetable); } - Console.ReadLine(); } + Console.ReadLine(); } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs index 84e00d47..49a10de3 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Validations.cs @@ -1,37 +1,31 @@ using Spectre.Console; -using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace ExerciseTracker.UI +namespace ExerciseTracker.UI; + +internal class Validations { - internal class Validations + internal static DateTime GetValidDate() { - internal static DateTime GetValidDate() + AnsiConsole.MarkupLine("[darkred]Enter the Date in 'dd-MM-yyyy' Format:[/]"); + bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateTime DateResult); + while (!res) { - AnsiConsole.MarkupLine("[darkred]Enter the Date in 'dd-MM-yyyy' Format:[/]"); - bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateTime DateResult); - while (!res) - { - AnsiConsole.MarkupLine("[darkred]Enter the Date in correct [yellow] 'dd-MM-yyyy'[/] Format:[/]"); - res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateResult); - } - return DateResult; + AnsiConsole.MarkupLine("[darkred]Enter the Date in correct [yellow] 'dd-MM-yyyy'[/] Format:[/]"); + res = DateTime.TryParseExact(Console.ReadLine().Trim(), "dd-MM-yyyy", null, DateTimeStyles.None, out DateResult); } + return DateResult; + } - internal static DateTime GetValidTime() + internal static DateTime GetValidTime() + { + AnsiConsole.MarkupLine("[lightsteelblue]Enter the Time in 'HH:mm' Format:[/]"); + bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out DateTime TimeResult); + while (!res) { - AnsiConsole.MarkupLine("[lightsteelblue]Enter the Time in 'HH:mm' Format:[/]"); - bool res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out DateTime TimeResult); - while (!res) - { - AnsiConsole.MarkupLine("[red3_1]Enter the Time in correct [yellow] 'HH:mm'[/] Format:[/]"); - res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out TimeResult); - } - return TimeResult; + AnsiConsole.MarkupLine("[red3_1]Enter the Time in correct [yellow] 'HH:mm'[/] Format:[/]"); + res = DateTime.TryParseExact(Console.ReadLine().Trim(), "HH:mm", null, DateTimeStyles.None, out TimeResult); } + return TimeResult; } } From 51e92c97b1c54677628181df71d0cf025e32c50a Mon Sep 17 00:00:00 2001 From: HKHemanthsharma Date: Sat, 28 Jun 2025 13:39:23 +0530 Subject: [PATCH 3/7] Changed Variable Names to align with coding Conventions --- .../Exercisetracker.UI/HttpClientService.cs | 2 +- .../Repositories/Repository.cs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs index 528aae86..b22958a4 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/HttpClientService.cs @@ -9,7 +9,7 @@ public HttpClientService() { BaseUrl = ConfigurationManager.AppSettings["BaseUrl"]; } - public string GetBaseURL() + public string GetBaseUrl() { return BaseUrl; } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs index 8b5e5c5a..db7f9570 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs @@ -6,18 +6,18 @@ namespace ExerciseTracker.UI.Repositories; public class Repository where T : class { - public string BaseURL { get; set; } + public string BaseUrl { get; set; } HttpClientService ClientService = new(); HttpClient Client; public Repository() { if (typeof(T).Name == "ExerciseShiftDto") { - BaseURL = ClientService.GetBaseURL() + "ExerciseShift"; + BaseUrl = ClientService.GetBaseUrl() + "ExerciseShift"; } else { - BaseURL = ClientService.GetBaseURL() + typeof(T).Name; + BaseUrl = ClientService.GetBaseUrl() + typeof(T).Name; } Client = ClientService.GetHttpClient(); } @@ -27,8 +27,8 @@ public async Task> GetAllEntities() { //https://localhost:7249/api/Exercise //https://localhost:7249/api/ExerciseShift - string Stringresponse = await Client.GetStringAsync(BaseURL); - using (var response = await Client.GetStreamAsync(BaseURL)) + string Stringresponse = await Client.GetStringAsync(BaseUrl); + using (var response = await Client.GetStreamAsync(BaseUrl)) { ResponseDto GetResponse = JsonSerializer.Deserialize>(response); return GetResponse; @@ -50,7 +50,7 @@ public async Task> GetEntiryById(int? Id) { try { - using (var response = await Client.GetStreamAsync(BaseURL + $"/{Id}")) + using (var response = await Client.GetStreamAsync(BaseUrl + $"/{Id}")) { ResponseDto GetResponse = JsonSerializer.Deserialize>(response); return GetResponse; @@ -72,7 +72,7 @@ public async Task> CreateEntity(T Entity) { try { - var response = await Client.PostAsJsonAsync(BaseURL, Entity); + var response = await Client.PostAsJsonAsync(BaseUrl, Entity); using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); @@ -97,7 +97,7 @@ public async Task> UpdateEntity(T Entity, int? Id) { try { - var response = await Client.PutAsJsonAsync(BaseURL + $"/{Id}", Entity); + var response = await Client.PutAsJsonAsync(BaseUrl + $"/{Id}", Entity); using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); @@ -120,7 +120,7 @@ public async Task> DeleteEntity(int? Id) { try { - var response = await Client.DeleteAsync(BaseURL + $"/{Id}"); + var response = await Client.DeleteAsync(BaseUrl + $"/{Id}"); using (var StreamResponse = await response.Content.ReadAsStreamAsync()) { ResponseDto Response = await JsonSerializer.DeserializeAsync>(StreamResponse); From 5976b911c91bffa47af0be539ae9b5379510cf1f Mon Sep 17 00:00:00 2001 From: HKHemanthsharma <125248238+HKHemanthsharma@users.noreply.github.com> Date: Sat, 28 Jun 2025 13:59:19 +0530 Subject: [PATCH 4/7] Created Readme --- ExerciseTracker.HemanthSharma/Readme | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ExerciseTracker.HemanthSharma/Readme diff --git a/ExerciseTracker.HemanthSharma/Readme b/ExerciseTracker.HemanthSharma/Readme new file mode 100644 index 00000000..ff631b72 --- /dev/null +++ b/ExerciseTracker.HemanthSharma/Readme @@ -0,0 +1,64 @@ +# ExerciseTracker.Study + +## Overview + +**ExerciseTracker.Study** is a C# project designed to help users track and manage their exercise routines efficiently. This application provides tools to log workouts, monitor progress, and analyze fitness trends, making it ideal for individuals and groups aiming to improve their physical health. + +## Features + +- Log daily exercises with details such as type, duration, and intensity +- View and analyze workout history and trends +- Set and track fitness goals +- User-friendly interface +- Data persistence for workout records + +## Getting Started + +### Prerequisites + +- [.NET SDK](https://dotnet.microsoft.com/download) (version X.X or above) +- A compatible development environment (e.g., Visual Studio, VS Code) + +### Installation + +1. **Clone the repository:** + ```bash + git clone https://github.com/HKHemanthsharma/CodeReviews.Console.ExerciseTracker.git + cd ExerciseTracker.Study + ``` + +2. **Restore dependencies:** + ```bash + dotnet restore + ``` + +3. **Build the project:** + ```bash + dotnet build + ``` + +4. **Run the application:** + ```bash + dotnet run + ``` + +## Usage + +- Launch the application and follow the on-screen instructions to start logging workouts. +- Navigate through features to view progress reports and set new goals. + +## Contributing + +Contributions are welcome! Please open an issue or submit a pull request for any feature requests, bug reports, or improvements. + +## License + +This project is licensed under the [MIT License](LICENSE). + +## Maintainers + +- [HKHemanthsharma](https://github.com/HKHemanthsharma) + +--- + +*Happy exercising!* From 0ce77e8698e23f1d8c26aa4533260ec69c03ac74 Mon Sep 17 00:00:00 2001 From: HKHemanthsharma <125248238+HKHemanthsharma@users.noreply.github.com> Date: Sat, 28 Jun 2025 13:59:50 +0530 Subject: [PATCH 5/7] Rename Readme to Readme.md --- ExerciseTracker.HemanthSharma/{Readme => Readme.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ExerciseTracker.HemanthSharma/{Readme => Readme.md} (100%) diff --git a/ExerciseTracker.HemanthSharma/Readme b/ExerciseTracker.HemanthSharma/Readme.md similarity index 100% rename from ExerciseTracker.HemanthSharma/Readme rename to ExerciseTracker.HemanthSharma/Readme.md From 49d92c7e8d48e9f628eb3e63fab0feb89f23836d Mon Sep 17 00:00:00 2001 From: HKHemanthsharma <125248238+HKHemanthsharma@users.noreply.github.com> Date: Sat, 28 Jun 2025 14:07:06 +0530 Subject: [PATCH 6/7] Updated Readme.md --- ExerciseTracker.HemanthSharma/Readme.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ExerciseTracker.HemanthSharma/Readme.md b/ExerciseTracker.HemanthSharma/Readme.md index ff631b72..669e0056 100644 --- a/ExerciseTracker.HemanthSharma/Readme.md +++ b/ExerciseTracker.HemanthSharma/Readme.md @@ -13,6 +13,13 @@ - Data persistence for workout records ## Getting Started +## Build Instructions +### DataBaseConnectionString +Change the Connection string in appsettings.json in ExerciseTracker API project to map to your local Db and run migration. +### BaseUrl in in Console project +Chnage the BaseUrl in ExerciseTracker.UI project in app.Config file. to point to the localhost port where your webAPI project runs +### Running Both the Projects as Startup Projects +right click on any of project-> Configure StartUp projects-> select Multiple Startup projects-> select both the projects listed as Start projects ### Prerequisites From a393f432a596fcb8e55a8832593961ea8c0e97f8 Mon Sep 17 00:00:00 2001 From: HKHemanthsharma Date: Thu, 3 Jul 2025 21:52:50 +0530 Subject: [PATCH 7/7] adjusted the Spaces, removed the Unused Variables Codacy suggested. --- .../Controllers/ExerciseController.cs | 5 +++++ .../Controllers/ExerciseShiftController.cs | 5 +++++ .../HelperMethods/HelperClass.cs | 2 ++ .../Repositories/RepositoryClass.cs | 1 - .../Repositories/RepositoryDapper.cs | 2 -- .../Exercisetracker.UI/Repositories/Repository.cs | 9 +++++---- .../Exercisetracker.UI/Services/ExerciseService.cs | 4 ++-- .../Exercisetracker.UI/UserInputs.cs | 2 ++ .../Exercisetracker.UI/UserOutputs.cs | 1 + 9 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs index ed2d0a0e..4e6258a1 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseController.cs @@ -14,17 +14,20 @@ public ExerciseController(IService service) { Service = service; } + [HttpGet] public async Task>> GetAllExercises() { return await Service.GetAll(); } + [HttpGet] [Route("{Id:int}")] public async Task>> GetExerciseById([FromRoute] int Id) { return await Service.GetById(Id); } + [HttpPost] public async Task>> Create([FromBody] ExerciseDto NewExercise) { @@ -35,6 +38,7 @@ public async Task>> Create([FromBody] Exercis } ); } + [HttpPut] [Route("{Id:int}")] public async Task>> Update([FromRoute] int Id, [FromBody] ExerciseDto UpdateExerciseDto) @@ -46,6 +50,7 @@ public async Task>> Update([FromRoute] int Id }; return await Service.Update(UpdateExercise); } + [HttpDelete] [Route("{Id:int}")] public async Task>> Delete([FromRoute] int Id) diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs index 36bf2de5..5c4c38f6 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Controllers/ExerciseShiftController.cs @@ -15,17 +15,20 @@ public ExerciseShiftController(IService service) { ShiftService = service; } + [HttpGet] public async Task>> GetAllShifts() { return await ShiftService.GetAll(); } + [HttpGet] [Route("{Id:int}")] public async Task>> GetById([FromRoute] int Id) { return await ShiftService.GetById(Id); } + [HttpPost] public async Task>> CreateShift([FromBody] ExerciseShiftDto NewExerciseDto) { @@ -43,6 +46,7 @@ public async Task>> CreateShift([FromBod } return await ShiftService.Create(NewExerciseShift); } + [HttpPut] [Route("{Id:int}")] public async Task>> UpdateShift([FromRoute] int Id, [FromBody] ExerciseShiftDto UpdateExerciseDto) @@ -61,6 +65,7 @@ public async Task>> UpdateShift([FromRou UpdateExerciseShift.Id = Id; return await ShiftService.Update(UpdateExerciseShift); } + [HttpDelete] [Route("{Id:int}")] public async Task>> DeleteShift([FromRoute] int Id) diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs index 4001405d..12bdc40b 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/HelperMethods/HelperClass.cs @@ -12,9 +12,11 @@ public static ExerciseShift ConvertToExercise(ExerciseShiftDto ExerciseDto) { return null; } + DateTime ShiftStartTime = DateTime.ParseExact(ExerciseDto.StartTime, "HH:mm", null); DateTime ShiftEndTime = DateTime.ParseExact(ExerciseDto.EndTime, "HH:mm", null); DateTime ShiftDate = DateTime.ParseExact(ExerciseDto.ExerciseDate, "dd-MM-yyyy", null); + ExerciseShift NewExercise = new ExerciseShift { ExerciseId = ExerciseDto.ExerciseId, diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs index 3442477f..1cdd28a6 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryClass.cs @@ -78,7 +78,6 @@ public async Task> Delete(int Id) Data = [] }; } - } public async Task> GetAll() diff --git a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs index 11225ad9..f449118c 100644 --- a/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs +++ b/ExerciseTracker.HemanthSharma/ExerciseTracker.HemanthSharma/Repositories/RepositoryDapper.cs @@ -47,8 +47,6 @@ public async Task> Create(Exercise Entity) Message = e.Message }; } - - } public async Task> Delete(int Id) diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs index db7f9570..570f98fd 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Repositories/Repository.cs @@ -21,13 +21,11 @@ public Repository() } Client = ClientService.GetHttpClient(); } + public async Task> GetAllEntities() { try { - //https://localhost:7249/api/Exercise - //https://localhost:7249/api/ExerciseShift - string Stringresponse = await Client.GetStringAsync(BaseUrl); using (var response = await Client.GetStreamAsync(BaseUrl)) { ResponseDto GetResponse = JsonSerializer.Deserialize>(response); @@ -46,6 +44,7 @@ public async Task> GetAllEntities() }; } } + public async Task> GetEntiryById(int? Id) { try @@ -68,6 +67,7 @@ public async Task> GetEntiryById(int? Id) }; } } + public async Task> CreateEntity(T Entity) { try @@ -93,6 +93,7 @@ public async Task> CreateEntity(T Entity) } } + public async Task> UpdateEntity(T Entity, int? Id) { try @@ -116,6 +117,7 @@ public async Task> UpdateEntity(T Entity, int? Id) }; } } + public async Task> DeleteEntity(int? Id) { try @@ -139,5 +141,4 @@ public async Task> DeleteEntity(int? Id) }; } } - } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs index 6ab7af54..871eead7 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/Services/ExerciseService.cs @@ -41,7 +41,7 @@ public static void UpdateExercise() int? UserChoice = UserInputs.GetExerciseById(Exercises); ResponseDto Response = Repo.GetEntiryById(UserChoice).GetAwaiter().GetResult(); Exercise UpdatedExercise = UserInputs.GetUpdatedEntity(Response.Data); - ResponseDto DeleteResponse = Repo.UpdateEntity(UpdatedExercise, UserChoice).GetAwaiter().GetResult(); - UserOutputs.ShowResponse(Response); + ResponseDto UpdatedResponse = Repo.UpdateEntity(UpdatedExercise, UserChoice).GetAwaiter().GetResult(); + UserOutputs.ShowResponse(UpdatedResponse); } } diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs index 36b68dfe..18ba2708 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserInputs.cs @@ -16,6 +16,7 @@ public class UserInputs where T : class .AddChoices(UserChoices)); return Entities.Where(x => x.name == UserOption).Select(x => x.id).FirstOrDefault(); } + internal static int GetShiftById(List Entities) { if (Entities.Count() == 0) @@ -25,6 +26,7 @@ internal static int GetShiftById(List Entities) Console.ReadLine(); return -1; } + List UserChoices = new(); UserChoices = Entities.Select(x => $"ShiftId={x.Id};ShiftDate={x.ExerciseDate};ShiftStartTime={x.StartTime}").ToList(); var UserOption = AnsiConsole.Prompt(new SelectionPrompt().Title("Please Choose an option") diff --git a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs index b23f995c..9d79c56a 100644 --- a/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs +++ b/ExerciseTracker.HemanthSharma/Exercisetracker.UI/UserOutputs.cs @@ -34,6 +34,7 @@ public static void ShowResponse(ResponseDto Response) "DELETE" => "Details of the Entity Deleted", _ => "Unknown" }; + if (Response.Data.Count() == 0) { ResponseString = $"[yellow]Response Method:{Response.ResponseMethod}\nCurrently No Data Found For the requested Entityy in DataBase[/]";