public async Task <ActionResult <AuctionCreateDTO> > UpdateAuctionById(string id, AuctionCreateDTO newAuction) { var check = await _auctRepo.GetAuctionById(id); if (check != null) { var auction = new AppAuction { AuctionId = newAuction.AuctionId, SellerId = newAuction.SellerId, BuyerId = newAuction.BuyerId, CardId = newAuction.CardId, PriceSold = (double)newAuction.PriceSold, SellDate = (DateTime)newAuction.SellDate, //for auction details PriceListed = newAuction.PriceListed, BuyoutPrice = newAuction.BuyoutPrice, NumberBids = (int)newAuction.NumberBids, SellType = newAuction.SellType, ExpDate = newAuction.ExpDate }; bool result = await _auctRepo.UpdateAuction(id, auction); if (result) { return(NoContent()); //update successfull } else { return(BadRequest()); //something wrong with update } } return(NotFound()); //Return 404 if no auction details found }
//Get all auctions public async Task <IEnumerable <AppAuction> > GetAllAuctions() { var dbAuctions = await _context.Auctions.Include(x => x.AuctionDetail).ToListAsync(); if (dbAuctions == null) { return(null); } var appAuctions = new List <AppAuction>(); foreach (var dbAuction in dbAuctions) { var appAuction = new AppAuction() { AuctionId = dbAuction.AuctionId, SellerId = dbAuction.SellerId, BuyerId = dbAuction.BuyerId, CardId = dbAuction.CardId, PriceSold = (double)dbAuction.PriceSold, SellDate = (DateTime)dbAuction.SellDate, PriceListed = dbAuction.AuctionDetail.PriceListed, BuyoutPrice = dbAuction.AuctionDetail.BuyoutPrice, NumberBids = dbAuction.AuctionDetail.NumberBids, SellType = dbAuction.AuctionDetail.SellType, ExpDate = dbAuction.AuctionDetail.ExpDate }; //check if a buyer exists and set Buyer and Seller for Expiration check if (appAuction.BuyerId != "" && appAuction.BuyerId != null) { appAuction.Buyer = await _userRepo.GetOneUser(appAuction.BuyerId); } appAuction.Seller = await _userRepo.GetOneUser(appAuction.SellerId); appAuction.Card = await _cardRepo.GetOneCard(appAuction.CardId); //updates appAuction and users if UtcNow > ExpDate /*if(appAuction.Expired()) * { * await _userRepo.UpdateUserById(appAuction.BuyerId, appAuction.Buyer); * await _userRepo.UpdateUserById(appAuction.SellerId, appAuction.Seller); * await UpdateAuction(appAuction.AuctionId,appAuction); * //TODO:check in expired() and callback * if (appAuction.NumberBids != null) * { * await _userRepo.AddOneCardToOneUser(appAuction.BuyerId, appAuction.CardId); * await _userRepo.DeleteOneCardOfOneUser(appAuction.SellerId, appAuction.CardId); * } * await _context.SaveChangesAsync(); * }*/ appAuctions.Add(appAuction); } return(appAuctions); }
public async Task <ActionResult <AuctionCreateDTO> > Post(AuctionCreateDTO newAuction) { //check if auction exists var check = await _auctRepo.GetAuctionById(newAuction.AuctionId); if (check == null) { //for repo var createdAuction = new AppAuction() { // auction AuctionId = await _auctRepo.IdGen(), SellerId = newAuction.SellerId, BuyerId = null, CardId = newAuction.CardId, PriceSold = 0, //SellDate = (DateTime)newAuction.SellDate, //auction details PriceListed = newAuction.PriceListed, BuyoutPrice = newAuction.BuyoutPrice, NumberBids = 0, SellType = "", ExpDate = newAuction.ExpDate }; await _auctRepo.CreateAuction(createdAuction); //for response var auctionReadDTO = new AuctionReadDTO { //auction AuctionId = createdAuction.AuctionId, SellerId = createdAuction.SellerId, //BuyerId = createdAuction.BuyerId, CardId = createdAuction.CardId, //PriceSold = (double)createdAuction.PriceSold, //SellDate = (DateTime)createdAuction.SellDate, //auction details PriceListed = createdAuction.PriceListed, BuyoutPrice = createdAuction.BuyoutPrice, //NumberBids = (int)createdAuction.NumberBids, //SellType = createdAuction.SellType, ExpDate = createdAuction.ExpDate }; return(CreatedAtAction(nameof(GetAuctionById), new { id = auctionReadDTO.AuctionId }, auctionReadDTO)); //201 new auction created } return(Conflict()); //auction already exists and cant be created }
public static AppAuction GetAuctionDetail(AuctionDetail dbAuction) { var appAuctionDetail = new AppAuction() { AuctionId = dbAuction.AuctionId, PriceListed = dbAuction.PriceListed, BuyoutPrice = dbAuction.BuyoutPrice, NumberBids = dbAuction.NumberBids, SellType = dbAuction.SellType, ExpDate = dbAuction.ExpDate }; return(appAuctionDetail); }
//auctions /* public static IEnumerable<AppAuction> GetAllAuctions(IEnumerable<Auction> dbAuctions, IEnumerable<AuctionDetail> dbAuctionDetails) * { * var appAuctions = new List<AppAuction>(); * * foreach (var dbAuction in dbAuctions) * { * var appAuction = new AppAuction() * { * AuctionId = dbAuction.AuctionId, * SellerId = dbAuction.SellerId, * BuyerId = dbAuction.BuyerId, * CardId = dbAuction.CardId, * PriceSold = (double)dbAuction.PriceSold, * SellDate = (DateTime)dbAuction.SellDate, * PriceListed = dbAuctionDetail.PriceListed, * BuyoutPrice = dbAuctionDetail.BuyoutPrice, * NumberBids = dbAuctionDetail.NumberBids, * SellType = dbAuctionDetail.SellType, * ExpDate = dbAuctionDetail.ExpDate * }; * appAuctions.Add(appAuction); * } * * return appAuctions; * }*/ public static AppAuction GetAuction(Auction dbAuction) { var appAuction = new AppAuction() { AuctionId = dbAuction.AuctionId, SellerId = dbAuction.SellerId, BuyerId = dbAuction.BuyerId, CardId = dbAuction.CardId, PriceSold = (double)dbAuction.PriceSold, SellDate = (DateTime)dbAuction.SellDate }; return(appAuction); }
//Create an auction public async Task <bool> CreateAuction(AppAuction auction) { if (auction == null) { return(false); } else { //create db objects var dbAuction = new Auction { AuctionId = auction.AuctionId, SellerId = auction.SellerId, BuyerId = auction.BuyerId, CardId = auction.CardId, PriceSold = auction.PriceSold, SellDate = auction.SellDate }; var dbAuctionDetail = new AuctionDetail { AuctionId = auction.AuctionId, PriceListed = auction.PriceListed, BuyoutPrice = auction.BuyoutPrice, NumberBids = auction.NumberBids, SellType = auction.SellType, ExpDate = auction.ExpDate }; //Add to db try { await _context.Auctions.AddAsync(dbAuction); await _context.AuctionDetails.AddAsync(dbAuctionDetail); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Debug.WriteLine("Error Creating Auction: " + e); } } return(false); }
//Perform any business logic on passed AppAuction, then enter into db TODO: move to controller/domain public async Task <bool> UpdateAuction(string id, AppAuction auction) { var updateAuction = await _context.Auctions.Include(x => x.AuctionDetail).Where(x => x.AuctionId == id).FirstAsync(); if (auction == null) { return(false); } if (auction.BuyerId != "" && auction.BuyerId != null) { auction.Buyer = await _userRepo.GetOneUser(auction.BuyerId); } auction.Seller = await _userRepo.GetOneUser(auction.SellerId); auction.Card = await _cardRepo.GetOneCard(auction.CardId); //if there is a Price sold, and PriceSold == BuyoutPrice buyout the auction if (auction.PriceSold == auction.BuyoutPrice && updateAuction.PriceSold != auction.PriceSold) { auction.BuyOut(); await _userRepo.AddOneCardToOneUser(auction.BuyerId, auction.CardId); await _userRepo.DeleteOneCardOfOneUser(auction.SellerId, auction.CardId); } //if the auction had a new priceListed then it has been bid on else if (auction.PriceListed != updateAuction.AuctionDetail.PriceListed) { double bid = auction.PriceListed - updateAuction.AuctionDetail.PriceListed; auction.NewBid(DateTime.UtcNow, bid); } try { //update db record updateAuction.SellerId = auction.SellerId; updateAuction.BuyerId = auction.BuyerId; updateAuction.CardId = auction.CardId; updateAuction.PriceSold = auction.PriceSold; updateAuction.SellDate = auction.SellDate; //update details updateAuction.AuctionDetail.PriceListed = auction.PriceListed; updateAuction.AuctionDetail.BuyoutPrice = auction.BuyoutPrice; updateAuction.AuctionDetail.NumberBids = auction.NumberBids; updateAuction.AuctionDetail.SellType = auction.SellType; updateAuction.AuctionDetail.ExpDate = auction.ExpDate; //save updated record if (auction.BuyerId != "" && auction.BuyerId != null) { await _userRepo.UpdateUserById(auction.BuyerId, auction.Buyer); } await _userRepo.UpdateUserById(auction.SellerId, auction.Seller); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { Debug.WriteLine("Error Updating Auction: " + e); } return(false); }