public void UpdateResturantDetails() { bool result = false; IResturant resturantModel = null; try { IResturant updatedResturant = null; resturantModel = SetupAddedResturant(); int size = _random.Next(0, 30); string name = GenerateResturantName(size); resturantModel.ResturantName = name; _resturantController.UpdateResturantDetails(JsonConvert.SerializeObject(resturantModel)); using (var dbContext = new ResturantReviewDBContext()) { updatedResturant = dbContext.Resturants.Where(x => x.ResturantName.Equals(name)).FirstOrDefault(); result = updatedResturant != 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); }
public void SearchForResturant() { bool result = false; IResturant resturantModel = null; try { int size = _random.Next(0, 30); string desiredName = GenerateResturantName(size); resturantModel = SetupAddedResturant(desiredName); var searchResults = _resturantController.SearchResturant(resturantModel.ResturantName); result = CheckSearchResultsList(resturantModel.ResturantName, searchResults); using (var dbContext = new ResturantReviewDBContext()) { 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); }
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); }
public void DeleteResturant() { bool result = false; IResturant resturantModel = null; try { Resturant deletedResturant = null; resturantModel = SetupAddedResturant("testModel"); using (var _dbContext = new ResturantReviewDBContext()) { _resturantController.DeleteResturant(resturantModel.ResturantId); deletedResturant = _dbContext.Resturants.Where(x => x.ResturantName.Equals("testModel")).FirstOrDefault(); result = deletedResturant == null; if (!result) { _dbContext.Resturants.Remove(Resturant.ConvertToEntity(resturantModel)); _dbContext.SaveChanges(); } } } catch (Exception) { try { using (var dbContext = new ResturantReviewDBContext()) { dbContext.Resturants.Remove(Resturant.ConvertToEntity(resturantModel)); dbContext.SaveChanges(); } } catch (DbUpdateConcurrencyException) { } } Assert.True(result); }
public bool UpdateResturantDetails([FromRoute] dynamic resturantJson) { IResturant resturant = null; try { resturant = JsonConvert.DeserializeObject <Resturant>(resturantJson); using (var _dbContext = new ResturantReviewDBContext()) { if (resturant != null) { _dbContext.Resturants.Update(Resturant.ConvertToEntity(resturant)); _dbContext.SaveChanges(); } } return(true); } catch (Exception e) { _logger.LogError(e.Message); return(false); } }
public bool DeleteResturant([FromRoute] int resturantId) { IResturant resturant; try { using (var _dbContext = new ResturantReviewDBContext()) { resturant = _dbContext.Resturants.Where(x => x.ResturantId == resturantId).FirstOrDefault(); if (resturant != null) { _dbContext.Entry(resturant).State = EntityState.Detached; _dbContext.Resturants.Remove(Resturant.ConvertToEntity(resturant)); _dbContext.SaveChanges(); } } return(true); } catch (Exception e) { _logger.LogError(e.Message); return(false); } }