diff --git a/Controllers/UserController.cs b/Controllers/UserController.cs index 77e437a..bc4ef8b 100644 --- a/Controllers/UserController.cs +++ b/Controllers/UserController.cs @@ -50,6 +50,18 @@ namespace FirmTracker_Server.Controllers } return Ok(roleClaim); } + [HttpGet("emails")] + [Authorize(Roles = Roles.Admin)] + public ActionResult> GetAllUserEmails() + { + var emails = UserService.GetAllUserEmails(); + if (emails == null || !emails.Any()) + { + return NotFound("No users found or unable to retrieve emails."); + } + + return Ok(emails); + } // New method to get all users /* [HttpGet("all")] [AllowAnonymous] diff --git a/Controllers/WorkDayController.cs b/Controllers/WorkDayController.cs index 844d115..ba1a5f9 100644 --- a/Controllers/WorkDayController.cs +++ b/Controllers/WorkDayController.cs @@ -15,6 +15,8 @@ * along with FirmTracker - Server. If not, see . */ +using FirmTracker_Server.Entities; +using FirmTracker_Server.Models; using FirmTracker_Server.nHibernate; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -74,6 +76,8 @@ namespace FirmTracker_Server.Controllers } } + + // Endpoint to get all workdays for a user [HttpGet("user/{userMail}/workdays")] [Authorize(Roles = Roles.Admin + "," + Roles.User)] @@ -89,6 +93,39 @@ namespace FirmTracker_Server.Controllers return BadRequest(new { message = "An error occurred while fetching workdays.", error = ex.Message }); } } + [HttpPost("absence/add")] + [Authorize(Roles = Roles.Admin + "," + Roles.User)] + public IActionResult AddAbsence([FromBody] AddAbsenceDto dto) + { + try + { + if (string.IsNullOrEmpty(dto.userEmail)) + { + return BadRequest(new { message = "User email must be provided." }); + } + + // Fetch the userId based on the provided email + int userId; + using (var session = SessionFactory.OpenSession()) + { + var user = session.Query().FirstOrDefault(u => u.Email == dto.userEmail); + if (user == null) + { + return NotFound(new { message = "User with the given email not found." }); + } + userId = user.UserId; + } + + // Add the absence for the retrieved userId + _workdayCRUD.AddAbsence(userId, dto.AbsenceType, dto.StartTime, dto.EndTime); + + return Ok(new { status = "added", userId, dto.userEmail, absenceType = dto.AbsenceType }); + } + catch (Exception ex) + { + return BadRequest(new { message = "An error occurred while adding the absence.", error = ex.Message }); + } + } } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9e47bb3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# Step 1: Use the official .NET SDK image to build the app +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /src + +# Copy the project file and restore dependencies +COPY ["FirmTracker-Server.csproj", "FirmTracker-Server/"] +RUN dotnet restore "FirmTracker-Server/FirmTracker-Server.csproj" + +# Copy the rest of the application code +WORKDIR "/src/FirmTracker-Server" +COPY . . + + +# Copy the szyfrowanie.dll into the build directory (to ensure it's available during the build) +COPY ["szyfrowanie.dll", "./"] + +# Build the app +RUN dotnet build "FirmTracker-Server.csproj" -c Release -o /app/build + +# Step 2: Publish the app +FROM build AS publish +RUN dotnet publish "FirmTracker-Server.csproj" -c Release -o /app/publish + +# Step 3: Create the final image using a runtime-only image +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +WORKDIR /app +EXPOSE 80 +EXPOSE 443 + +# Copy the published app from the previous stage +COPY --from=publish /app/publish . + +# Copy the szyfrowanie.dll to the final image (if needed at runtime) +COPY ["szyfrowanie.dll", "./"] + +# Set the entry point for the container +ENTRYPOINT ["dotnet", "FirmTracker-Server.dll"] diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj index 05849f9..3ec9a97 100644 --- a/FirmTracker-Server.csproj +++ b/FirmTracker-Server.csproj @@ -1,7 +1,7 @@ - net8.0-windows + net8.0 enable enable FirmTracker_Server @@ -37,7 +37,7 @@ - ..\..\..\Desktop\szyfrowanie.dll + ./szyfrowanie.dll diff --git a/Models/AddAbsenceDtocs.cs b/Models/AddAbsenceDtocs.cs new file mode 100644 index 0000000..ba80d3d --- /dev/null +++ b/Models/AddAbsenceDtocs.cs @@ -0,0 +1,12 @@ +namespace FirmTracker_Server.Models +{ + public class AddAbsenceDto + { + public string userEmail { get; set; } + public string AbsenceType { get; set; } // e.g., "Sick", "Vacation", etc. + public DateTime StartTime { get; set; } + public DateTime EndTime { get; set; } + + } + +} diff --git a/Models/UpdateAbsenceDto.cs b/Models/UpdateAbsenceDto.cs new file mode 100644 index 0000000..db32c06 --- /dev/null +++ b/Models/UpdateAbsenceDto.cs @@ -0,0 +1,10 @@ +namespace FirmTracker_Server.Models +{ + public class UpdateAbsenceDto + { + public string NewAbsenceType { get; set; } // e.g., "Sick", "Vacation", etc. + public DateTime NewStartTime { get; set; } + public DateTime NewEndTime { get; set; } + + } +} diff --git a/Program.cs b/Program.cs index 4a73767..355a24c 100644 --- a/Program.cs +++ b/Program.cs @@ -46,10 +46,10 @@ namespace FirmTracker_Server internal static class Program { - public static async Task Main(string[] args) + public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); - string appDirectory = Directory.GetCurrentDirectory(); + string appDirectory = Directory.GetCurrentDirectory(); string configFilePath = Path.Combine(appDirectory, "appsettings.json"); string connectionString = ""; if (File.Exists(configFilePath)) @@ -61,7 +61,7 @@ namespace FirmTracker_Server var connectionstringsection = config.GetSection("AppSettings:ConnectionString"); connectionString = connectionstringsection.Value; - + //Console.WriteLine(connectionString); SessionFactory.Init(connectionString); } else @@ -87,7 +87,7 @@ namespace FirmTracker_Server { options.JsonSerializerOptions.Converters.Add(new DateTimeConverter()); }); - ; + ; builder.ConfigureAuthentication(); builder.Services.AddAuthorization(); builder.Services.AddEndpointsApiExplorer(); @@ -104,18 +104,18 @@ namespace FirmTracker_Server .AddJsonFile("appsettings.json") .Build(); - - var port = configSwagger.GetValue("Port", 5075); + + var port = configSwagger.GetValue("Port", 5075); var port2 = configSwagger.GetValue("Port", 7039); - app.Urls.Add($"http://*:{port}"); - app.Urls.Add($"https://*:{port2}"); - + app.Urls.Add($"http://*:{port}"); + app.Urls.Add($"https://*:{port2}"); + try { app.UseSwagger(); app.UseSwaggerUI(c => { - c.SwaggerEndpoint($"/swagger/v1/swagger.json", "FirmTracker - TEST"); + c.SwaggerEndpoint($"/swagger/v1/swagger.json", "FirmTracker - TEST"); c.RoutePrefix = "swagger"; }); Console.WriteLine("uruchomiono swaggera"); @@ -125,6 +125,7 @@ namespace FirmTracker_Server { Console.WriteLine("Nie uda³o siê uruchomiæ swaggera"); } + app.UseHttpsRedirection(); app.UseCors("AllowSpecificOrigin"); @@ -133,7 +134,7 @@ namespace FirmTracker_Server app.UseAuthentication(); app.UseAuthorization(); - + app.MapControllers(); var configuration = new Configuration(); diff --git a/Services/UserService.cs b/Services/UserService.cs index a846b49..0957662 100644 --- a/Services/UserService.cs +++ b/Services/UserService.cs @@ -23,7 +23,7 @@ namespace FirmTracker_Server.Services UserDto GetById(int id); int AddUser(CreateUserDto dto); string CreateTokenJwt(LoginDto dto); - + IEnumerable GetAllUserEmails(); } public class UserService : IUserService @@ -44,7 +44,15 @@ namespace FirmTracker_Server.Services SimplerAES = new SimplerAES(); //SessionFactory = sessionFactory; } - + public IEnumerable GetAllUserEmails() + { + using (var session = SessionFactory.OpenSession()) + { + // Query the users and return a list of emails + var users = session.Query().Select(u => u.Email).ToList(); + return users; + } + } public UserDto GetById(int id) { using (var session = SessionFactory.OpenSession()) diff --git a/appsettings.json b/appsettings.json index f9a565b..180832f 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,6 +1,6 @@ { "AppSettings": { - "ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;" + "ConnectionString": "Server=localhost,1433;Initial Catalog=master;User Id=sa;Password=Rap45tro2;" }, "TokenConfig": { @@ -17,14 +17,7 @@ "applicationUrl": "http://localhost:5045" }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "https://localhost:7039" - - }, + "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, diff --git a/nHIbernate/Workday.cs b/nHIbernate/Workday.cs index ac85b39..f8ea1c8 100644 --- a/nHIbernate/Workday.cs +++ b/nHIbernate/Workday.cs @@ -20,5 +20,6 @@ namespace FirmTracker_Server.nHibernate } } public virtual User User { get; set; } + public virtual string Absence { get; set; } } } diff --git a/nHIbernate/WorkdayMapping.cs b/nHIbernate/WorkdayMapping.cs index 5cdbfde..49af811 100644 --- a/nHIbernate/WorkdayMapping.cs +++ b/nHIbernate/WorkdayMapping.cs @@ -10,6 +10,7 @@ namespace FirmTracker_Server.nHibernate Map(x => x.StartTime); Map(x => x.EndTime); References(x => x.User).Column("UserId"); // Assuming Workday is related to a User + Map(x => x.Absence); } } } diff --git a/nHIbernate/WorkdayRepository.cs b/nHIbernate/WorkdayRepository.cs index eff5486..2a6dc7d 100644 --- a/nHIbernate/WorkdayRepository.cs +++ b/nHIbernate/WorkdayRepository.cs @@ -30,7 +30,8 @@ public class WorkdayRepository var workday = new Workday { StartTime = DateTime.Now, - User = user + User = user, + Absence = "" }; session.Save(workday); @@ -43,6 +44,36 @@ public class WorkdayRepository } } } + + public void AddAbsence(int userId, string absenceType, DateTime startTime, DateTime endTime) + { + using (var session = SessionFactory.OpenSession()) + using (var transaction = session.BeginTransaction()) + { + try + { + var user = session.Get(userId); + if (user == null) throw new Exception("User not found"); + + // Create a new workday entry for the absence + var workday = new Workday + { + User = user, + StartTime = startTime, + EndTime = endTime, + Absence = absenceType // Store the absence type as a string + }; + + session.Save(workday); + transaction.Commit(); + } + catch (Exception ex) + { + transaction.Rollback(); + throw new Exception("An error occurred while adding the absence", ex); + } + } + } public bool StopWorkday(int userId) { @@ -90,6 +121,7 @@ public class WorkdayRepository StartTime = w.StartTime, EndTime = w.EndTime ?? DateTime.Today.AddHours(17), WorkedHours = (w.EndTime ?? DateTime.Today.AddHours(17)) - w.StartTime, + Absence = w.Absence, }) .ToList(); diff --git a/nHibernate/Transactions/TransactionCRUD.cs b/nHibernate/Transactions/TransactionCRUD.cs index 9be2044..feab4de 100644 --- a/nHibernate/Transactions/TransactionCRUD.cs +++ b/nHibernate/Transactions/TransactionCRUD.cs @@ -293,7 +293,7 @@ namespace FirmTracker_Server.nHibernate.Transactions } // Remove the product from the transaction - transaction.TotalPrice -= (transactionProduct.Quantity * product.Price * (1 - (transaction.Discount / 100))); + transaction.TotalPrice = (transaction.TotalPrice * (1 + (transaction.Discount / 100))) - (transactionProduct.Quantity * product.Price ); transaction.TotalPrice = Math.Round(transaction.TotalPrice, 2, MidpointRounding.AwayFromZero); // Remove the product from the Transaction's Product list diff --git a/szyfrowanie.dll b/szyfrowanie.dll new file mode 100644 index 0000000..450df76 Binary files /dev/null and b/szyfrowanie.dll differ