public bool AddResturant([FromRoute] string resturantName, [FromRoute] string unitNumber, [FromRoute] string buildingNumber, [FromRoute] string streetName, [FromRoute] string suburbName, [FromRoute] string cityName, [FromRoute] string stateName, [FromRoute] string countryName, [FromRoute] string postcode, [FromRoute] string ownerName, [FromRoute] string description, [FromRoute] int resturantType, [FromRoute] byte[] image = null) { try { // If a whitespace is given for the address then it was meant to be null if (string.IsNullOrWhiteSpace(unitNumber)) { unitNumber = null; } FormalAddress resturantAddress = new FormalAddress() { AddressApartmentNumber = unitNumber, AddressBuildingNumber = buildingNumber, AddressStreetName = streetName, AddressSuburbName = suburbName, AddressCityName = cityName, AddressStateName = stateName, AddressCountryName = countryName, AddressPostcode = postcode }; Resturant resturant = new Resturant() { ResturantName = resturantName, ResturantAddress = resturantAddress, ResturantImage = image, ResturantOwnerName = ownerName, ResturantDescription = description, ResturantType = (ResturantType)resturantType, DateAdded = DateTime.Now }; using (var _dbContext = new ResturantReviewDBContext()) { _dbContext.Add(resturant); _dbContext.SaveChanges(); } return(true); } catch (Exception e) { _logger.LogError(e.Message); return(false); } }
public void AddResturant() { bool result = false; IResturant resturantModel = null; try { var resturant = new Mock <IResturant>(); var address = new FormalAddress(); resturant.SetupAllProperties(); resturant.SetupProperty(x => x.ResturantAddress, address); resturantModel = resturant.Object; var addressModel = resturant.Object.ResturantAddress; _resturantController.AddResturant(resturantModel.ResturantName, addressModel.AddressApartmentNumber, addressModel.AddressBuildingNumber, addressModel.AddressStreetName, addressModel.AddressSuburbName, addressModel.AddressCityName, addressModel.AddressStateName, addressModel.AddressCountryName, addressModel.AddressPostcode, resturantModel.ResturantOwnerName, resturantModel.ResturantDescription, (int)resturantModel.ResturantType, null); Resturant dbResturantModel; using (var dbContext = new ResturantReviewDBContext()) { dbResturantModel = dbContext.Resturants.Where(x => x.DuplicateEquals(resturantModel)).FirstOrDefault(); result = dbResturantModel != null; dbContext.Resturants.Remove(dbResturantModel); dbContext.SaveChanges(); // Ensures test object is removed if (!result && resturantModel != null) { dbContext.Resturants.Remove(Resturant.ConvertToEntity(resturantModel)); dbContext.SaveChanges(); } } } catch (Exception) { using (var dbContext = new ResturantReviewDBContext()) { dbContext.Resturants.Remove(Resturant.ConvertToEntity(resturantModel)); dbContext.SaveChanges(); } } Assert.True(result); }
// Adds a test resturant to the database private IResturant SetupAddedResturant(string desiredName = "") { IResturant resturant = new Resturant(); var address = new FormalAddress(); IResturant resturantModel = null; resturant.ResturantAddress = address; if (!string.IsNullOrEmpty(desiredName)) { resturant.ResturantName = desiredName; } using (var dbContext = new ResturantReviewDBContext()) { dbContext.Resturants.Add(Resturant.ConvertToEntity(resturant)); dbContext.SaveChanges(); resturantModel = dbContext.Resturants.Where(x => x.ResturantName.Equals(resturant.ResturantName)).FirstOrDefault(); } return(resturantModel); }