PI2024-30 #6
@ -5,6 +5,7 @@ using System.Linq;
|
|||||||
using FirmTracker_Server.nHibernate;
|
using FirmTracker_Server.nHibernate;
|
||||||
using FirmTracker_Server.nHibernate.Expenses;
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
using FirmTracker_Server.nHibernate.Transactions;
|
using FirmTracker_Server.nHibernate.Transactions;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using QuestPDF.Fluent;
|
using QuestPDF.Fluent;
|
||||||
using QuestPDF.Helpers;
|
using QuestPDF.Helpers;
|
||||||
@ -14,6 +15,7 @@ namespace FirmTracker_Server.Controllers
|
|||||||
{
|
{
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
|
[Authorize]
|
||||||
public class PdfController : ControllerBase
|
public class PdfController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IExpenseRepository _expenseRepository;
|
private readonly IExpenseRepository _expenseRepository;
|
||||||
@ -26,6 +28,7 @@ namespace FirmTracker_Server.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("download")]
|
[HttpGet("download")]
|
||||||
|
[Authorize(Roles = Roles.Admin)]
|
||||||
public IActionResult DownloadReport(
|
public IActionResult DownloadReport(
|
||||||
[FromQuery] string reportType, // "expenses" or "transactions"
|
[FromQuery] string reportType, // "expenses" or "transactions"
|
||||||
[FromQuery] DateTime? startDate,
|
[FromQuery] DateTime? startDate,
|
||||||
|
@ -58,11 +58,18 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
foreach (var product in transaction.TransactionProducts)
|
foreach (var product in transaction.TransactionProducts)
|
||||||
{
|
{
|
||||||
|
// Validate if the product quantity is positive
|
||||||
|
if (product.Quantity <= 0)
|
||||||
|
{
|
||||||
|
return BadRequest($"Ilość na produktu {product.ProductName} musi być dodatnia.");
|
||||||
|
}
|
||||||
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
||||||
if (productByName == null)
|
if (productByName == null)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Produkt o nazwie {product.ProductName} nie istnieje.");
|
throw new InvalidOperationException($"Produkt o nazwie {product.ProductName} nie istnieje.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
product.ProductID = productByName.Id;
|
product.ProductID = productByName.Id;
|
||||||
product.TransactionId = transaction.Id;
|
product.TransactionId = transaction.Id;
|
||||||
|
|
||||||
@ -132,6 +139,11 @@ namespace FirmTracker_Server.Controllers
|
|||||||
{
|
{
|
||||||
foreach (var product in transaction.TransactionProducts)
|
foreach (var product in transaction.TransactionProducts)
|
||||||
{
|
{
|
||||||
|
// Validate if the product quantity is positive
|
||||||
|
if (product.Quantity <= 0)
|
||||||
|
{
|
||||||
|
return BadRequest($"Sprzedawana ilość produktu {product.ProductName} musi być ilością dodatnią.");
|
||||||
|
}
|
||||||
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
||||||
if (productByName == null)
|
if (productByName == null)
|
||||||
{
|
{
|
||||||
@ -207,5 +219,27 @@ namespace FirmTracker_Server.Controllers
|
|||||||
return Ok(transactions);
|
return Ok(transactions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DELETE: api/Transaction/5/product/10
|
||||||
|
[HttpDelete("{transactionId}/product/{productId}")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
|
||||||
|
public IActionResult DeleteTransactionProduct(int transactionId, int productId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_transactionCRUD.DeleteTransactionProduct(transactionId, productId);
|
||||||
|
return NoContent(); // Successfully removed the product
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ioe)
|
||||||
|
{
|
||||||
|
return BadRequest(ioe.Message); // If the transaction or product isn't found
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return NotFound(ex.Message); // Other general errors
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ namespace FirmTracker_Server.Controllers
|
|||||||
|
|
||||||
public WorkdayController()
|
public WorkdayController()
|
||||||
{
|
{
|
||||||
_workdayCRUD = new WorkdayRepository(); // Instantiate directly (no DI in this example)
|
_workdayCRUD = new WorkdayRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Endpoint to start a workday
|
// Endpoint to start a workday
|
||||||
@ -44,14 +44,51 @@ namespace FirmTracker_Server.Controllers
|
|||||||
{
|
{
|
||||||
var userIdString = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
var userIdString = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||||
int userId = int.Parse(userIdString);
|
int userId = int.Parse(userIdString);
|
||||||
|
|
||||||
|
// Attempt to start a new workday
|
||||||
_workdayCRUD.StartWorkday(userId);
|
_workdayCRUD.StartWorkday(userId);
|
||||||
return Ok(new { status = "started", userId });
|
return Ok(new { status = "started", userId });
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
// If there's an error (like previous workday not stopped), handle it
|
||||||
return BadRequest(new { message = "An error occurred while starting the workday.", error = ex.Message });
|
return BadRequest(new { message = "An error occurred while starting the workday.", error = ex.Message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Endpoint to stop a workday
|
||||||
|
[HttpPost("stop")]
|
||||||
|
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
|
||||||
|
public IActionResult StopWorkday()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var userIdString = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
|
||||||
|
int userId = int.Parse(userIdString);
|
||||||
|
|
||||||
|
var result = _workdayCRUD.StopWorkday(userId);
|
||||||
|
return Ok(new { status = result ? "stopped" : "already stopped", userId });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { message = "An error occurred while stopping the workday.", error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint to get all workdays for a user
|
||||||
|
[HttpGet("user/{userMail}/workdays")]
|
||||||
|
[Authorize(Roles = Roles.Admin + "," + Roles.User)]
|
||||||
|
public IActionResult GetWorkdays(string userMail)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var workdays = _workdayCRUD.GetWorkdaysByUser(userMail);
|
||||||
|
return Ok(workdays);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { message = "An error occurred while fetching workdays.", error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ namespace YourNamespace.Models
|
|||||||
public virtual int Id { get; set; }
|
public virtual int Id { get; set; }
|
||||||
public virtual DateTime? StartTime { get; set; }
|
public virtual DateTime? StartTime { get; set; }
|
||||||
public virtual DateTime? EndTime { get; set; }
|
public virtual DateTime? EndTime { get; set; }
|
||||||
|
public TimeSpan WorkedHours { get; set; }
|
||||||
// Many-to-One relationship to the User entity
|
// Many-to-One relationship to the User entity
|
||||||
public virtual User User { get; set; }
|
public virtual User User { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -239,9 +239,9 @@ namespace FirmTracker_Server
|
|||||||
expenseCrud.AddExpense(expense3);
|
expenseCrud.AddExpense(expense3);
|
||||||
|
|
||||||
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
||||||
new TransactionProduct { ProductID =17, Quantity = 10 },
|
new TransactionProduct { ProductID =17, Quantity = 3 },
|
||||||
new TransactionProduct { ProductID = 14, Quantity = 1 },
|
new TransactionProduct { ProductID = 14, Quantity = 1 },
|
||||||
new TransactionProduct { ProductID = 1, Quantity = 0 },
|
new TransactionProduct { ProductID = 1, Quantity = 1 },
|
||||||
};
|
};
|
||||||
foreach (var transactionProduct in testTransactionProducts)
|
foreach (var transactionProduct in testTransactionProducts)
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Retrieve a specific transaction by ID
|
|
||||||
public Transaction GetTransaction(int transactionId)
|
public Transaction GetTransaction(int transactionId)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
@ -53,7 +53,7 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve transactions within a specific date range
|
|
||||||
public List<Transaction> GetTransactionsByDateRange(DateTime startDate, DateTime endDate)
|
public List<Transaction> GetTransactionsByDateRange(DateTime startDate, DateTime endDate)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
@ -64,7 +64,7 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve all products for a specific transaction
|
|
||||||
public List<TransactionProduct> GetTransactionProducts(int transactionId)
|
public List<TransactionProduct> GetTransactionProducts(int transactionId)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
@ -75,7 +75,7 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a new transaction
|
|
||||||
public void AddTransaction(Transaction transaction)
|
public void AddTransaction(Transaction transaction)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
@ -113,7 +113,7 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a transaction by ID
|
|
||||||
public void DeleteTransaction(int transactionId)
|
public void DeleteTransaction(int transactionId)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
@ -7,6 +7,18 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
public virtual int Id { get; set; }
|
public virtual int Id { get; set; }
|
||||||
public virtual DateTime StartTime { get; set; }
|
public virtual DateTime StartTime { get; set; }
|
||||||
public virtual DateTime? EndTime { get; set; } // Nullable EndTime, if not finished
|
public virtual DateTime? EndTime { get; set; } // Nullable EndTime, if not finished
|
||||||
public virtual User User { get; set; } // Assuming a relationship to a User entity
|
public virtual TimeSpan WorkedHours
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
// Calculate the worked hours, using 5 PM as the fallback for the EndTime
|
||||||
|
return (EndTime ?? DateTime.Today.AddHours(24)) - StartTime;
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public virtual User User { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,103 @@
|
|||||||
using FirmTracker_Server.Entities;
|
using FirmTracker_Server.Entities;
|
||||||
using NHibernate;
|
using FirmTracker_Server.nHibernate;
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace FirmTracker_Server.nHibernate
|
public class WorkdayRepository
|
||||||
{
|
{
|
||||||
public class WorkdayRepository
|
public void StartWorkday(int userId)
|
||||||
{
|
{
|
||||||
public void StartWorkday(int userId)
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transaction = session.BeginTransaction())
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
try
|
||||||
using (var transaction = session.BeginTransaction())
|
|
||||||
{
|
{
|
||||||
try
|
// Check if there is an existing workday that hasn't been stopped yet
|
||||||
{
|
var ongoingWorkday = session.Query<Workday>()
|
||||||
// Fetch the user entity by its ID
|
.Where(w => w.User.UserId == userId && w.EndTime == null)
|
||||||
var user = session.Get<User>(userId); // Assuming User is a mapped entity
|
.OrderByDescending(w => w.StartTime)
|
||||||
if (user == null)
|
.FirstOrDefault();
|
||||||
{
|
|
||||||
throw new Exception("User not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new Workday and assign the User reference
|
if (ongoingWorkday != null)
|
||||||
var workday = new Workday
|
|
||||||
{
|
|
||||||
StartTime = DateTime.Now,
|
|
||||||
User = user // Set the User reference here
|
|
||||||
};
|
|
||||||
|
|
||||||
session.Save(workday);
|
|
||||||
transaction.Commit();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
transaction.Rollback();
|
// If there is an ongoing workday, throw an exception or return a specific message
|
||||||
throw new Exception("An error occurred while starting the workday", ex);
|
throw new Exception("Previous workday wasn't stopped yet.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch the user entity
|
||||||
|
var user = session.Get<User>(userId);
|
||||||
|
if (user == null) throw new Exception("User not found");
|
||||||
|
|
||||||
|
// Create a new workday if there is no ongoing one
|
||||||
|
var workday = new Workday
|
||||||
|
{
|
||||||
|
StartTime = DateTime.Now,
|
||||||
|
User = user
|
||||||
|
};
|
||||||
|
|
||||||
|
session.Save(workday);
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw new Exception("An error occurred while starting the workday", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool StopWorkday(int userId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var transaction = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var workday = session.Query<Workday>()
|
||||||
|
.Where(w => w.User.UserId == userId && w.EndTime == null)
|
||||||
|
.OrderByDescending(w => w.StartTime)
|
||||||
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
if (workday == null)
|
||||||
|
{
|
||||||
|
return false; // No ongoing workday found
|
||||||
|
}
|
||||||
|
|
||||||
|
workday.EndTime = DateTime.Now;
|
||||||
|
|
||||||
|
session.Update(workday);
|
||||||
|
transaction.Commit();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw new Exception("An error occurred while stopping the workday", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Workday> GetWorkdaysByUser(string email)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var workdays = session.Query<Workday>()
|
||||||
|
.Where(w => w.User.Email == email)
|
||||||
|
.Select(w => new Workday
|
||||||
|
{
|
||||||
|
Id = w.Id,
|
||||||
|
StartTime = w.StartTime,
|
||||||
|
EndTime = w.EndTime ?? DateTime.Today.AddHours(17),
|
||||||
|
WorkedHours = (w.EndTime ?? DateTime.Today.AddHours(17)) - w.StartTime,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return workdays;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception("An error occurred while fetching workdays", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,6 +94,10 @@ namespace FirmTracker_Server.nHibernate.Transactions
|
|||||||
{
|
{
|
||||||
var product = session.Get<Product>(tp.ProductID);
|
var product = session.Get<Product>(tp.ProductID);
|
||||||
|
|
||||||
|
if(tp.Quantity < 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
if (product.Type != 0)
|
if (product.Type != 0)
|
||||||
{
|
{
|
||||||
product.Availability += tp.Quantity;
|
product.Availability += tp.Quantity;
|
||||||
@ -253,6 +257,63 @@ namespace FirmTracker_Server.nHibernate.Transactions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void DeleteTransactionProduct(int transactionId, int productId)
|
||||||
|
{
|
||||||
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
using (var t = session.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Get the transaction to update
|
||||||
|
var transaction = session.Get<Transaction>(transactionId);
|
||||||
|
if (transaction == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Transaction with ID {transactionId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the transaction product to remove
|
||||||
|
var transactionProduct = transaction.TransactionProducts.FirstOrDefault(tp => tp.ProductID == productId);
|
||||||
|
if (transactionProduct == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Product with ID {productId} not found in the transaction.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the product to update availability
|
||||||
|
var product = session.Get<Product>(productId);
|
||||||
|
if (product == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Product with ID {productId} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revert the product availability
|
||||||
|
if (product.Type != 0)
|
||||||
|
{
|
||||||
|
product.Availability += transactionProduct.Quantity;
|
||||||
|
session.Update(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the product from the transaction
|
||||||
|
transaction.TotalPrice -= (transactionProduct.Quantity * product.Price * (1 - (transaction.Discount / 100)));
|
||||||
|
transaction.TotalPrice = Math.Round(transaction.TotalPrice, 2, MidpointRounding.AwayFromZero);
|
||||||
|
|
||||||
|
// Remove the product from the Transaction's Product list
|
||||||
|
transaction.TransactionProducts.Remove(transactionProduct);
|
||||||
|
|
||||||
|
// Now delete the transaction product
|
||||||
|
session.Delete(transactionProduct);
|
||||||
|
|
||||||
|
// Update the transaction total price
|
||||||
|
session.Update(transaction);
|
||||||
|
|
||||||
|
t.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
t.Rollback();
|
||||||
|
throw new InvalidOperationException($"Error while deleting product from transaction: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public IList<Transaction2> GetAllTransactions()
|
public IList<Transaction2> GetAllTransactions()
|
||||||
|
Loading…
Reference in New Issue
Block a user