private Response HandleTradeDeal(Request request, string dealId) { var dealer = new TradeDealsRepository(); string username = Session.GetUsername(ExtractAuthorizationToken(request.Authorization)); if (!dealer.DealExists(dealId)) { return(ResponseManager.NotFound($"deal {dealId} does not exist")); } var deal = dealer.GetDeal(dealId); var users = new UsersRepository(); if (deal.OwnerId == users.GetUserID(username)) { return(ResponseManager.BadRequest("you can't trade with yourself")); } if (request.Payload.IsNullOrWhiteSpace()) { return(ResponseManager.BadRequest("no payload")); } string offeredCardId = request.Payload.Replace('\"', ' ').Trim(); var cards = new CardsRepository(); if (!cards.CardExists(offeredCardId)) { return(ResponseManager.BadRequest($"the offered card {offeredCardId} does not exist")); } if (!cards.InStack(username, offeredCardId)) { return(ResponseManager.BadRequest($"user {username} is the not the owner of offered card {offeredCardId}")); } var offeredCard = cards.GetCard(offeredCardId); if (new DecksRepository().HasCardInDeck(username, offeredCardId)) { return(ResponseManager.BadRequest($"card {deal.CardId} is in the deck for {username}. You can't trade cards in the deck")); } if (OfferMeatsDealRequirements(offeredCard, deal)) { cards.TransferOwnership(deal.OwnerId, offeredCardId); cards.TransferOwnership(username, deal.CardId); dealer.RemoveDeal(deal.Id); return(ResponseManager.Created($"trade deal successfully closed.")); } return(ResponseManager.BadRequest($"Deal requirements not met!")); }