Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions LuveAPI/Controllers/AuctionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AuctionDto> auctions = _auctionService.GetAuctionsCreatedByUserId(userId);

List<AuctionViewModel> 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)
{
Expand Down
10 changes: 6 additions & 4 deletions LuveBLL/Dto/CreateEditAuctionDto.cs
Original file line number Diff line number Diff line change
@@ -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.")]
Expand Down
1 change: 1 addition & 0 deletions LuveBLL/Interfaces/Repositories/IAuctionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IAuctionRepository
AuctionDto UpdateAuction(int id, CreateEditAuctionDto createEditAuctionDto);
bool DeleteAuction(int id);
int UpdateExpiredAuctions();
List<AuctionDto> GetAuctionsByCreatorId(string userId);
}
}
1 change: 1 addition & 0 deletions LuveBLL/Interfaces/Services/IAuctionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface IAuctionService
bool UpdateCurrentPrice(int id, int currentPrice);
bool UpdateAuctionStatus(int id, AuctionStatusDto status);
bool DeleteAuction(int id);
List<AuctionDto> GetAuctionsCreatedByUserId(string userId);
}
}
5 changes: 5 additions & 0 deletions LuveBLL/Services/AuctionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public bool UpdateAuctionStatus(int id, AuctionStatusDto status)
return true;
}

public List<AuctionDto> GetAuctionsCreatedByUserId(string userId)
{
return _auctionRepository.GetAuctionsByCreatorId(userId);
}

public bool DeleteAuction(int id)
{
return _auctionRepository.DeleteAuction(id);
Expand Down
29 changes: 29 additions & 0 deletions LuveDAL/AuctionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ public bool DeleteAuction(int id)
return true;
}

public List<AuctionDto> GetAuctionsByCreatorId(string userId)
{
var auctions = _context.Auctions
.Where(a => a.UserId == userId)
.Include(a => a.User)
.ToList();

List<AuctionDto> 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
Expand Down