Exemplo n.º 1
0
		public HttpResponseMessage AddRestaurant( Restaurant restaurant ) {
			CrudResult result = new CrudResult();
			using ( Repo repo = new Repo() ) {
				result = repo.SaveRestaurant( restaurant );
				string json = JsonConvert.SerializeObject( result, jss );
				HttpResponseMessage msg = Request.CreateResponse( HttpStatusCode.OK, json );
				return msg;
			}
		}
Exemplo n.º 2
0
		public void ModelTest_AddRestaurant() {
			// create a new restaurant
			Restaurant r = new Restaurant() { Name = "Testaurant", Category = "Category", City = "Butler", State = "PA", Street = "100 Main Street", ZipCode = "16001" };

			// test model
			using ( Repo repo = new Repo() ) {
				CrudResult result = repo.SaveRestaurant( r );
				Assert.IsTrue( result.Success, "Add restaurant did not succeed." );
			}

			// delete test restaurant
			using ( SuprContext ctx = new SuprContext() ) {
				ctx.Restaurant.Attach( r );
				ctx.Restaurant.Remove( r );
				ctx.SaveChanges();
			}
		}
Exemplo n.º 3
0
		public CrudResult SaveRestaurant( Restaurant restaurant ) {
			CrudResult result = new CrudResult();

			if ( restaurant.Id > 0 )
				ctx.Entry( restaurant ).State = EntityState.Modified;
			else
				ctx.Restaurant.Add( restaurant );

			try {
				ctx.SaveChanges();
				result.Success = true;
			}
			catch ( DbEntityValidationException ex ) {
				result.Errors = ex.EntityValidationErrors;
				result.Success = false;
			}

			return result;
		}
Exemplo n.º 4
0
		public ItemResult<Restaurant> GetRestaurantById( int id ) {
			ItemResult<Restaurant> result = new ItemResult<Restaurant>();

			Restaurant item = new Restaurant();
			try {
				item = ctx.Restaurant.Where( r => r.Id == id ).FirstOrDefault();

				if ( item != null )
					result.Item = item;
				else {
					result.Status = "No restaurants found with Id = " + id;
				}
			}
			catch ( Exception except ) {
				result.Status = except.InnerException != null ? except.InnerException.Message : except.Message;
			}

			return result;
		}
Exemplo n.º 5
0
		public void ModelTest_GetRestaurantByCity() {
			// add restaurant to guarantee a find
			Restaurant r = new Restaurant() { Name = "Testaurant", City = "butLEr", State = "PA" };
			using ( SuprContext ctx = new SuprContext() ) {
				ctx.Restaurant.Add( r );
				ctx.SaveChanges();

				// test the repository
				string city = "BUtler";
				using ( Repo repo = new Repo() ) {
					ListResult<Restaurant> restaurants = repo.GetRestaurantsByCity( city );
					Assert.IsNotNull( restaurants, "No restaurants in " + city + " found." );
				}

				// delete the created restaurant
				r = ctx.Restaurant.Where( d => d.Id == r.Id ).First();
				ctx.Restaurant.Remove( r );
				ctx.SaveChanges();
			}
		}
Exemplo n.º 6
0
		public void ModelTest_GetRestaurantById() {
			// add restaurant to guarantee a find
			Restaurant r = new Restaurant() { Name = "Testaurant", City = "butLEr", State = "PA" };
			using ( SuprContext ctx = new SuprContext() ) {
				ctx.Restaurant.Add( r );
				ctx.SaveChanges();
				int id = r.Id;

				// test the repository
				using ( Repo repo = new Repo() ) {
					ItemResult<Restaurant> restaurant = repo.GetRestaurantById( id );
					Assert.IsNotNull( restaurant, "No restaurant with Id = " + restaurant.Item.Id + " found." );
				}

				// delete the created restaurant
				r = ctx.Restaurant.Where( d => d.Id == id ).First();
				ctx.Restaurant.Remove( r );
				ctx.SaveChanges();
			}
		}
Exemplo n.º 7
0
		public void ApiTest_GetRestaurantByCity() {
			// add restaurant to guarantee a find
			Restaurant r = new Restaurant() { Name = "Testaurant", City = "butLEr", State = "PA" };
			using ( SuprContext ctx = new SuprContext() ) {
				ctx.Restaurant.Add( r );
				ctx.SaveChanges();
				string city = "BUtler";

				// test the api
				var token = ApiClient.GetAPIToken( usr, pwd, uri ).Result;
				var response = ApiClient.GetRequest( token, uri, "/api/v1/GetRestaurant?City=" + city ).Result;

				ListResult<Restaurant> result = JsonConvert.DeserializeObject<ListResult<Restaurant>>( response.ToString() );
				Assert.IsNotNull( result, "No restaurants were found in " + city + "." );

				// delete the created restaurant
				r = ctx.Restaurant.Where( d => d.Id == r.Id ).First();
				ctx.Restaurant.Remove( r );
				ctx.SaveChanges();
			}
		}
Exemplo n.º 8
0
		public void ApiTest_GetRestaurantById() {
			// add restaurant to guarantee a find
			Restaurant r = new Restaurant() { Name = "Testaurant", City = "butLEr", State = "PA" };
			using ( SuprContext ctx = new SuprContext() ) {
				ctx.Restaurant.Add( r );
				ctx.SaveChanges();
				int id = r.Id;

				// test the api
				var token = ApiClient.GetAPIToken( usr, pwd, uri ).Result;
				var response = ApiClient.GetRequest( token, uri, "/api/v1/GetRestaurant?Id=" + id ).Result;

				ItemResult<Restaurant> result = JsonConvert.DeserializeObject<ItemResult<Restaurant>>( response.ToString() );
				Assert.IsNotNull( result, "No restaurant with Id = " + result.Item.Id + " found." );

				// delete the created restaurant
				r = ctx.Restaurant.Where( d => d.Id == id ).First();
				ctx.Restaurant.Remove( r );
				ctx.SaveChanges();
			}
		}