diff --git a/LuveAPI/Controllers/AuctionsController.cs b/LuveAPI/Controllers/AuctionsController.cs index e957140..a4d87af 100644 --- a/LuveAPI/Controllers/AuctionsController.cs +++ b/LuveAPI/Controllers/AuctionsController.cs @@ -134,6 +134,40 @@ public IActionResult GetAuctionByUser() return Ok(auctionViewModels); } + [Authorize] + [HttpGet("user/created")] + public IActionResult GetAuctionsCreatedByUser() + { + var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; + if (userId == null) + { + return Unauthorized(); + } + + List auctions = _auctionService.GetAuctionsCreatedByUserId(userId); + + List auctionViewModels = auctions.Select(auction => new AuctionViewModel + { + Id = auction.Id, + Title = auction.Title, + Description = auction.Description, + StartingPrice = auction.StartingPrice, + CurrentPrice = auction.CurrentPrice, + Increment = auction.Increment, + Status = auction.Status, + ImageUrl = auction.ImageUrl, + User = new UserViewModel + { + Id = auction.User.Id, + Username = auction.User.Username, + Email = auction.User.Email, + }, + EndDate = auction.EndDate, + }).ToList(); + + return Ok(auctionViewModels); + } + [HttpPatch("{id}/status")] public IActionResult UpdateAuctionStatus(int id, string status) { diff --git a/LuveBLL/Dto/CreateEditAuctionDto.cs b/LuveBLL/Dto/CreateEditAuctionDto.cs index 0bb96ef..b371f44 100644 --- a/LuveBLL/Dto/CreateEditAuctionDto.cs +++ b/LuveBLL/Dto/CreateEditAuctionDto.cs @@ -1,10 +1,12 @@ using System; using System.ComponentModel.DataAnnotations; -namespace LuveBLL.Dto { - public class CreateEditAuctionDto { - - [Required(ErrorMessage = "Title is required.")] +namespace LuveBLL.Dto +{ + public class CreateEditAuctionDto + { + + [Required(ErrorMessage = "Title is requiredd.")] [StringLength(100, ErrorMessage = "Title cannot be longer than 100 characters.")] public string Title { get; set; } [Required(ErrorMessage = "Description is required.")] diff --git a/LuveBLL/Interfaces/Repositories/IAuctionRepository.cs b/LuveBLL/Interfaces/Repositories/IAuctionRepository.cs index 54c828f..e0a315b 100644 --- a/LuveBLL/Interfaces/Repositories/IAuctionRepository.cs +++ b/LuveBLL/Interfaces/Repositories/IAuctionRepository.cs @@ -11,5 +11,6 @@ public interface IAuctionRepository AuctionDto UpdateAuction(int id, CreateEditAuctionDto createEditAuctionDto); bool DeleteAuction(int id); int UpdateExpiredAuctions(); + List GetAuctionsByCreatorId(string userId); } } \ No newline at end of file diff --git a/LuveBLL/Interfaces/Services/IAuctionService.cs b/LuveBLL/Interfaces/Services/IAuctionService.cs index 0f25a93..f9ff832 100644 --- a/LuveBLL/Interfaces/Services/IAuctionService.cs +++ b/LuveBLL/Interfaces/Services/IAuctionService.cs @@ -13,5 +13,6 @@ public interface IAuctionService bool UpdateCurrentPrice(int id, int currentPrice); bool UpdateAuctionStatus(int id, AuctionStatusDto status); bool DeleteAuction(int id); + List GetAuctionsCreatedByUserId(string userId); } } \ No newline at end of file diff --git a/LuveBLL/Services/AuctionService.cs b/LuveBLL/Services/AuctionService.cs index 77535e7..85f7573 100644 --- a/LuveBLL/Services/AuctionService.cs +++ b/LuveBLL/Services/AuctionService.cs @@ -102,6 +102,11 @@ public bool UpdateAuctionStatus(int id, AuctionStatusDto status) return true; } + public List GetAuctionsCreatedByUserId(string userId) + { + return _auctionRepository.GetAuctionsByCreatorId(userId); + } + public bool DeleteAuction(int id) { return _auctionRepository.DeleteAuction(id); diff --git a/LuveDAL/AuctionRepository.cs b/LuveDAL/AuctionRepository.cs index 7a2146a..6feec71 100644 --- a/LuveDAL/AuctionRepository.cs +++ b/LuveDAL/AuctionRepository.cs @@ -162,6 +162,35 @@ public bool DeleteAuction(int id) return true; } + public List GetAuctionsByCreatorId(string userId) + { + var auctions = _context.Auctions + .Where(a => a.UserId == userId) + .Include(a => a.User) + .ToList(); + + List auctionDtos = auctions.Select(auction => new AuctionDto + { + Id = auction.Id, + Title = auction.Title, + Description = auction.Description, + StartingPrice = auction.Starting_price, + CurrentPrice = auction.Current_price, + Increment = auction.Increment, + Status = (AuctionStatusDto)auction.Status, + ImageUrl = auction.Image_url, + User = new UserDto + { + Id = auction.User.Id, + Username = auction.User.UserName, + Email = auction.User.Email, + }, + EndDate = auction.End_date, + }).ToList(); + + return auctionDtos; + } + public int UpdateExpiredAuctions() { var expiredAuctions = _context.Auctions