Beispiel #1
0
        public ActionResult Create([Bind(Include = "userID,is_public,optIn,showEthnicity,showAge,showCountry,showGender,description,profileImgUrl")] UserProfile userProfile)
        {
            if (ModelState.IsValid)
            {
                db.UserProfiles.Add(userProfile);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.userID = new SelectList(db.AspNetUsers, "Id", "Email", userProfile.userID);
            return(View(userProfile));
        }
Beispiel #2
0
        public ActionResult Create([Bind(Include = "listID,userID,listName")] UserList userList)
        {
            userList.userID = User.Identity.GetUserId();
            if (ModelState.IsValid)
            {
                db.UserLists.Add(userList);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(userList));
        }
Beispiel #3
0
        //[ValidateAntiForgeryToken]
        public ContentResult Create(int usdaFoodID, int listID, string brandOwner, string upc, string description)
        {
            if (User.Identity.GetUserId() != null)
            {
                SavedFood savedFood = new SavedFood(usdaFoodID, listID, brandOwner, upc, description);
                try
                {
                    db.SavedFoods.Add(savedFood);
                    db.SaveChanges();
                    var jsonData = new
                    {
                        success  = true,
                        message  = "Food has been saved.",
                        redirect = false
                    };

                    var result = JObject.FromObject(jsonData);
                    return(Content(result.ToString(), "Application/json"));
                }
                catch
                {
                    var jsonData2 = new
                    {
                        success  = false,
                        message  = "Food has already been saved.",
                        redirect = false
                    };

                    var result2 = JObject.FromObject(jsonData2);
                    return(Content(result2.ToString(), "Application/json"));
                }
            }

            var jsonData3 = new
            {
                success  = false,
                message  = "User not logged in.",
                redirect = true
            };

            var result3 = JObject.FromObject(jsonData3);

            return(Content(result3.ToString(), "Application/json"));
        }
Beispiel #4
0
 public ActionResult AddFodmap([Bind(Include = "Name,Aliases")] FODMAPIngredient newIngredient, string previousAction)
 {
     if (ModelState.IsValid)
     {
         if (db.FODMAPIngredients.FirstOrDefault(x => x.Name.Contains(newIngredient.Name)) != null ||
             db.FODMAPIngredients.FirstOrDefault(x => x.Aliases.Contains(newIngredient.Name)) != null)
         {
             return(RedirectToAction(previousAction, new { Message = Message.FodmapExists }));
         }
         db.FODMAPIngredients.Add(newIngredient);
         db.SaveChanges();
         return(RedirectToAction(previousAction, new { Message = Message.AddFodmapSuccess }));
     }
     return(RedirectToAction("AddFodmap", new { Message = Message.AddFodmapFailure }));
 }
Beispiel #5
0
        public IHttpActionResult Create([FromBody] SavedFoodRequest request)
        {
            using (FFDBContext db = new FFDBContext())
            {
                if (User.Identity.GetUserId() != null)
                {
                    SavedFood savedFood = new SavedFood(request.UsdaFoodID, request.ListID, request.BrandOwner, request.Upc, request.Description);
                    try
                    {
                        db.SavedFoods.Add(savedFood);
                        db.SaveChanges();
                        var jsonData = new JsonResponse
                        {
                            success  = true,
                            message  = "Food has been saved.",
                            redirect = false
                        };

                        return(Ok(jsonData));
                    }
                    catch
                    {
                        var jsonData2 = new JsonResponse
                        {
                            success  = false,
                            message  = "Food has already been saved.",
                            redirect = false
                        };

                        return(Ok(jsonData2));
                    }
                }

                var jsonData3 = new JsonResponse
                {
                    success  = false,
                    message  = "User not logged in.",
                    redirect = true
                };

                return(Ok(jsonData3));
            }
        }
Beispiel #6
0
 public bool AddCompany(string companyName, string companyCode)
 {
     using (FFDBContext context = new FFDBContext())
     {
         try
         {
             context.Companies.Add(new Company()
             {
                 Code = companyCode,
                 Name = companyName
             });
             context.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             Logger.Error("添加货代公司失败", ex);
             return false;
         }
     }
 }
Beispiel #7
0
 public bool AddRegCode(string machineCode, string regCode, int companyId)
 {
     using (FFDBContext context = new FFDBContext())
     {
         try
         {
             context.RegisterCodes.Add(new RegisterCode()
             {
                 MachineCode = machineCode,
                 RegCode = regCode,
                 CompanyId = companyId,
                 CreatedDate = DateTime.Now,
                 State = (int)RegCodeStates.Actived
             });
             context.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             Logger.Error("添加注册码失败", ex);
             return false;
         }
     }
 }
Beispiel #8
0
        public IHttpActionResult Create([FromBody] LabelIngredientRequest req)
        {
            var ingredientName = req.IngredientName;
            var assignLabel    = req.AssignLabel;

            using (FFDBContext db = new FFDBContext())
            {
                string userID = User.Identity.GetUserId();
                if (userID != null)
                {
                    int ingredientID = db.LabelledIngredients.Where(s => s.Name == ingredientName).Select(s => s.ID).FirstOrDefault();
                    if (ingredientID == 0)
                    {
                        LabelledIngredient labelIngredient = new LabelledIngredient {
                            Name = ingredientName
                        };
                        try
                        {
                            db.LabelledIngredients.Add(labelIngredient);
                            db.SaveChanges();
                            ingredientID = labelIngredient.ID;
                        }
                        catch
                        {
                            var jsonData_failed_ingredient_add = new JsonResponse
                            {
                                success  = false,
                                message  = "Ingredient not added to database",
                                redirect = false
                            };


                            return(Ok(jsonData_failed_ingredient_add));
                        }
                    }
                    if (db.UserIngredients.Where(s => s.LabelledIngredientID == ingredientID && s.userID == userID).Count() > 0)
                    {
                        var existingRecord = db.UserIngredients.FirstOrDefault(s => s.LabelledIngredientID == ingredientID && s.userID == userID);
                        existingRecord.Label = assignLabel;
                        db.SaveChanges();
                        var jsonData_edit_record = new JsonResponse
                        {
                            success  = true,
                            message  = "Label has been changed in the record",
                            redirect = false
                        };

                        return(Ok(jsonData_edit_record));
                    }
                    else
                    {
                        UserIngredient userIng = new UserIngredient(userID, assignLabel, ingredientID);
                        try
                        {
                            db.UserIngredients.Add(userIng);
                            db.SaveChanges();
                            var jsonData_success = new JsonResponse
                            {
                                success  = true,
                                message  = "Label has been saved.",
                                redirect = false
                            };

                            return(Ok(jsonData_success));
                        }
                        catch (Exception e)
                        {
                            var jsonData_fail = new JsonResponse
                            {
                                success  = false,
                                message  = $"Something went wrong: {e.InnerException.InnerException.Message}",
                                redirect = false
                            };

                            return(Ok(jsonData_fail));
                        }
                    }
                }

                var jsonData3 = new JsonResponse
                {
                    success  = false,
                    message  = "User not logged in.",
                    redirect = true
                };

                return(Ok(jsonData3));
            }
        }
Beispiel #9
0
        //[ValidateAntiForgeryToken]
        public ContentResult Create(string assignLabel, string ingredientName)
        {
            string userID = User.Identity.GetUserId();

            if (userID != null)
            {
                int ingredientID = db.LabelledIngredients.Where(s => s.Name == ingredientName).Select(s => s.ID).FirstOrDefault();
                if (ingredientID == 0)
                {
                    LabelledIngredient labelIngredient = new LabelledIngredient();
                    labelIngredient.Name = ingredientName;
                    try
                    {
                        db.LabelledIngredients.Add(labelIngredient);
                        db.SaveChanges();
                        ingredientID = labelIngredient.ID;
                    }
                    catch
                    {
                        var jsonData_failed_ingredient_add = new
                        {
                            success  = false,
                            message  = "Ingredient not added to database",
                            redirect = false
                        };

                        var result = JObject.FromObject(jsonData_failed_ingredient_add);
                        return(Content(result.ToString(), "Application/json"));
                    }
                }
                if (db.UserIngredients.Where(s => s.LabelledIngredientID == ingredientID && s.userID == userID).Count() > 0)
                {
                    var existingRecord = db.UserIngredients.FirstOrDefault(s => s.LabelledIngredientID == ingredientID && s.userID == userID);
                    existingRecord.Label = assignLabel;
                    db.SaveChanges();
                    var jsonData_edit_record = new
                    {
                        success  = true,
                        message  = "Label has been changed in the record",
                        redirect = false
                    };

                    var result = JObject.FromObject(jsonData_edit_record);
                    return(Content(result.ToString(), "Application/json"));
                }
                else
                {
                    UserIngredient userIng = new UserIngredient(userID, assignLabel, ingredientID);
                    try
                    {
                        db.UserIngredients.Add(userIng);
                        db.SaveChanges();
                        var jsonData_success = new
                        {
                            success  = true,
                            message  = "Label has been saved.",
                            redirect = false
                        };

                        var result = JObject.FromObject(jsonData_success);
                        return(Content(result.ToString(), "Application/json"));
                    }
                    catch
                    {
                        var jsonData_fail = new
                        {
                            success  = false,
                            message  = "Something went wrong",
                            redirect = false
                        };

                        var result2 = JObject.FromObject(jsonData_fail);
                        return(Content(result2.ToString(), "Application/json"));
                    }
                }
            }

            var jsonData3 = new
            {
                success  = false,
                message  = "User not logged in.",
                redirect = true
            };

            var result3 = JObject.FromObject(jsonData3);

            return(Content(result3.ToString(), "Application/json"));
        }
Beispiel #10
0
        public bool AddRouteInformationItems(IEnumerable<RouteInformationItem> rlist)
        {
            using (FFDBContext context = new FFDBContext())
            {
                try
                {
                    foreach (RouteInformationItem item in rlist)
                    {
                        context.RouteItems.Add(item);
                    }
                    context.SaveChanges();

                    return true;
                }
                catch (Exception ex)
                {
                    Logger.Error("向数据库中添加新纪录失败", ex);
                    return false;
                }
            }
        }
Beispiel #11
0
        public bool ImportRouteInformationItems(IEnumerable<RouteInformationItem> importlist)
        {
            // insert列表
            IList<RouteInformationItem> insertlist = importlist.Where(ri => ri.Id == 0).ToList();
            // update列表
            IList<RouteInformationItem> updatelist = importlist.Where(ri => ri.Id != 0).ToList();

            using (FFDBContext context = new FFDBContext())
            {
                //设置的时间是300秒,即五分钟
                context.Database.CommandTimeout = 300;
                DbTransaction tran = null;
                try
                {
                    using (DbConnection conn = ((IObjectContextAdapter)context).ObjectContext.Connection)
                    {
                        conn.Open();
                        using (tran = conn.BeginTransaction())
                        {
                            // insert
                            foreach (RouteInformationItem item in insertlist)
                            {
                                item.CreateDate = DateTime.Now;
                                item.UpdateDate = DateTime.Now;
                                item.IsDeleted = false;

                                context.RouteItems.Add(item);
                            }

                            // 要更新的所有记录ID
                            int[] idInFiles = importlist.Where(ri => ri.Id != 0).Select(ri => ri.Id).ToArray();

                            // 数据库中要更新的数据
                            IEnumerable<RouteInformationItem> db_updatelist = context.RouteItems.Where(ri => idInFiles.Contains(ri.Id)).ToList();
                            foreach (RouteInformationItem item in updatelist)
                            {
                                RouteInformationItem dbitem = db_updatelist.Single(ri => ri.Id == item.Id);
                                dbitem.ShipName = item.ShipName;
                                dbitem.StartPort = item.StartPort;
                                dbitem.DestinationPort = item.DestinationPort;
                                dbitem.StartDay = item.StartDay;
                                dbitem.Price_20GP = item.Price_20GP;
                                dbitem.Price_40GP = item.Price_40GP;
                                dbitem.Price_40HQ = item.Price_40HQ;
                                dbitem.Nonstop = item.Nonstop;
                                dbitem.SailDayLength = item.SailDayLength;
                                dbitem.ValidDate = item.ValidDate;
                                dbitem.Remarks = item.Remarks;
                                dbitem.IsSingleContainer = item.IsSingleContainer;

                                dbitem.UpdateDate = DateTime.Now;
                            }

                            // 参与重新导入的所有企业ID
                            int[] companyIDs = importlist.Select(ri => ri.CompanyId).Distinct().ToArray<int>();
                            // 数据库中现存的所有要参与重新导入企业的旧的信息
                            int[] oldDestinationIDs = context.RouteItems.Where(ri => companyIDs.Contains<int>(ri.CompanyId)).Select(ri => ri.Id).ToArray<int>();
                            // 要删除的 数据库ID
                            int[] deleteIDs = oldDestinationIDs.Except(idInFiles).ToArray();

                            IEnumerable<RouteInformationItem> deletelist = context.RouteItems.Where(ri => deleteIDs.Contains(ri.Id)).ToList();
                            // delete
                            foreach (RouteInformationItem item in deletelist)
                            {
                                // context.RouteItems.Remove(item);
                                item.IsDeleted = true;
                                item.DeleteDate = DateTime.Now;
                            }

                            context.SaveChanges();

                            // 提交事务
                            tran.Commit();
                        }
                    }

                    return true;
                }
                catch (Exception ex)
                {
                    // 回滚事务
                    tran.Rollback();
                    Logger.Error("批量更新数据失败", ex);
                    return false;
                }
            }
        }
Beispiel #12
0
 public bool DeletePackage(int pid)
 {
     using (FFDBContext context = new FFDBContext())
     {
         try
         {
             var package = context.UpgradePackages.SingleOrDefault(p=>p.Id == pid);
             if (package != null) {
                 context.UpgradePackages.Remove(package);
                 context.SaveChanges();
                 return true;
             }
             return false;
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }
Beispiel #13
0
        public bool AssociatMachineAndRegCode(string machineCode, string regcode, int companyId)
        {
            using (FFDBContext context = new FFDBContext())
            {
                try
                {
                    RegisterCode entity = context.RegisterCodes.Where(rc => rc.MachineCode.ToLower().Equals(machineCode.ToLower())).FirstOrDefault();
                    if (entity != null)
                    {
                        entity.RegCode = regcode;
                        entity.CompanyId = companyId;
                        entity.State = (int)RegCodeStates.Actived;

                        context.SaveChanges();
                        return true;
                    }
                    return false;
                }
                catch (Exception ex)
                {
                    Logger.Error("服务端关联公司ID和机器码失败", ex);
                    return false;
                }
            }
        }
Beispiel #14
0
 public bool AddUpgradePackage(UpgradePackage entity)
 {
     using (FFDBContext context = new FFDBContext())
     {
         try
         {
             context.UpgradePackages.Add(entity);
             context.SaveChanges();
             return true;
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
 }