public async Task Test_CreateMatch_FailsWhenInvalidLeadID() { // Arrange await _apiFactory.SeedDatabase(async context => { context.Contractors.Add(new Lead.Api.DataModel.Models.Contractor { ContractorID = 5, CompanyName = "Test Contractor" }); context.Leads.Add(new Lead.Api.DataModel.Models.Lead { LeadID = 1, Name = "Phill", ZipCode = "60640" }); await context.SaveChangesAsync(); }); var client = _apiFactory.CreateLeadClient(); var matchRequest = new AddMatchRequest { LeadID = 2, ContractorID = 5, Price = 5 }; ApiException expectedException = null; // Act try { var match = await client.AddMatchAsync(matchRequest.LeadID, matchRequest); } catch (ApiException exception) { expectedException = exception; } // Assert Assert.IsNotNull(expectedException); Assert.AreEqual(StatusCodes.Status400BadRequest, expectedException.StatusCode); }
public async Task Test_CreateMatch() { // Arrange await _apiFactory.SeedDatabase(async context => { context.Contractors.Add(new Lead.Api.DataModel.Models.Contractor { ContractorID = 5, CompanyName = "Test Contractor" }); context.Leads.Add(new Lead.Api.DataModel.Models.Lead { LeadID = 1, Name = "Phill", ZipCode = "60640" }); await context.SaveChangesAsync(); }); var client = _apiFactory.CreateLeadClient(); var matchRequest = new AddMatchRequest { LeadID = 1, ContractorID = 5, Price = 5 }; // Act var match = await client.AddMatchAsync(matchRequest.LeadID, matchRequest); // Assert Assert.IsTrue(match.MatchID > 0); Assert.AreEqual(1, match.LeadID); Assert.AreEqual(5, match.ContractorID); }
public async Task <Match> AddMatch([FromRoute] long leadID, [FromBody] AddMatchRequest request) { var lead = await _context.Leads.FirstOrDefaultAsync(l => l.LeadID == leadID); if (lead == null) { throw new KeyNotFoundException($"Lead: {leadID} not found"); } var contractor = await _context.Contractors.FirstOrDefaultAsync(c => c.ContractorID == request.ContractorID); if (contractor == null) { throw new KeyNotFoundException($"Contractor: {request.ContractorID} not found"); } var match = new Match { ContractorID = request.ContractorID, LeadID = leadID, PriceOfLead = request.Price }; _context.Add(match); await _context.SaveChangesAsync(); return(match); }
/// <summary> /// Method that does the creation of the match propagated from the AddMatch endpoint /// </summary> public Response <bool> AddMatch([FromBody] AddMatchRequest request) { var response = new Response <bool>(); Guid id = Guid.NewGuid(); var match = new Matches { CourtId = Decimal.Parse(request.CourtId), MaxPlayers = request.MaxPlayers, StartTime = request.StartTime, EndTime = request.EndTime, Type = request.Type, Id = id.ToString(), }; try { var result = _repository.Add(match); _logger.LogInfo("new match created"); return(AddUserMatch(request.UserId, result.Id)); } catch (Exception ex) { _logger.LogError("match creation failed"); response.Messages.Add(new ResponseMessage { Type = Contracts.Enums.ResponseMessageEnum.Exception, Message = ex.Message, }); response.Status = System.Net.HttpStatusCode.InternalServerError; return(response); } }
public Response <bool> AddMatch([FromBody] AddMatchRequest request) { _logger.LogInfo("request for new match arrived"); var identity = HttpContext.User.Identity as ClaimsIdentity; if (identity != null) { request.UserId = identity.FindFirst("jti").Value; } var result = _matchManager.AddMatch(request); return(result); }