public ActionResult Upload(int?id)
        {
            Ingredient ingredient = new Ingredient();

            TextFieldParser parser = new TextFieldParser(@"C:\Users\Nicholas\Documents\Visual Studio 2015\Projects\Ingredients\test.csv");

            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            while (!parser.EndOfData)
            {
                //Process row
                string[] fields = parser.ReadFields();

                ingredient.Name     = fields[0];
                ingredient.Quantity = Convert.ToDecimal(fields[1]);
                ingredient.Units    = fields[2];
                ingredient.Sku      = fields[3];
                ingredient.Supplier = fields[4];

                if (ModelState.IsValid)
                {
                    db.Ingredients.Add(ingredient);
                    db.SaveChanges();
                }
            }
            parser.Close();

            return(RedirectToAction("Index"));
        }
示例#2
0
        public ActionResult Create([Bind(Include = "ID,Name,Source,Amount,Unit,RecipeId")] Ingredient ingredient)
        {
            if (ModelState.IsValid)
            {
                db.Ingredients.Add(ingredient);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(ingredient));
        }
示例#3
0
        /// <summary>
        /// Updates entry in the database
        /// </summary>
        /// <param name="id">Identification number of the ingredient to be updated</param>
        /// <param name="ingredient">New ingredient details</param>
        /// <returns>Confirmation message</returns>
        public HttpResponseMessage Put(int id, [FromBody] Ingredient ingredient)
        {
            try
            {
                using (IngredientDBContext dbContext = new IngredientDBContext())
                {
                    var entity = dbContext.Ingredients.FirstOrDefault(e => e.ID == id);

                    if (entity == null)
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Ingredient with the ID " + id.ToString() + " not found"));
                    }
                    else
                    {
                        entity.ID       = ingredient.ID;
                        entity.Name     = ingredient.Name;
                        entity.Calories = ingredient.Calories;

                        dbContext.SaveChanges();

                        return(Request.CreateResponse(HttpStatusCode.OK, entity));
                    }
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }
示例#4
0
 /// <summary>
 /// Deletes an ingredient from the sql database
 /// </summary>
 /// <param name="id">ID of the ingredient to be deleted</param>
 /// <returns></returns>
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (IngredientDBContext dbContext = new IngredientDBContext())
         {
             var entity = dbContext.Ingredients.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                    "Ingredient with Id = " + id.ToString() + " not found to delete"));
             }
             else
             {
                 dbContext.Ingredients.Remove(entity);
                 dbContext.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
示例#5
0
        /// <summary>
        /// Creates a new ingredient entry in the database
        /// </summary>
        /// <param name="ingredient">Ingredient to be added to the database</param>
        /// <returns>Confirmation message</returns>
        public HttpResponseMessage Post([FromBody] Ingredient ingredient)
        {
            try
            {
                using (IngredientDBContext dBContext = new IngredientDBContext())
                {
                    dBContext.Ingredients.Add(ingredient);
                    dBContext.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, ingredient);
                    message.Headers.Location = new Uri(Request.RequestUri + ingredient.ID.ToString());

                    return(message);
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
            }
        }