コード例 #1
0
        public ActionResult Validate(Application app, IList <HttpPostedFileBase> images, HttpPostedFileBase File, IList <Genre> genres)
        {
            var result = "";

            if (ModelState.IsValid)
            {
                try
                {
                    app.Approved = 0;
                    ApplicationDAO      dao = new ApplicationDAO();
                    Application         uniq = dao.IsUnique(app);
                    DeveloperDAO        ddao = new DeveloperDAO();
                    ImageDAO            idao = new ImageDAO();
                    ApplicationGenreDAO agdao = new ApplicationGenreDAO();
                    Developer           Dev = ddao.SearchById(int.Parse(Session["DevId"].ToString()));
                    bool ImgError = false, FileError = false, AGError = false;
                    if (Dev != null)
                    {
                        if (app.ReleaseDate.Year >= 1900 && app.ReleaseDate.Year <= DateTime.Now.Year + 1)
                        {
                            if (app.Price >= 0 && app.Price <= 1000)
                            {
                                if (uniq == null)
                                {
                                    app.DeveloperId = Dev.Id;
                                    dao             = new ApplicationDAO();
                                    dao.Add(app);
                                    Dev.NumSoft++;
                                    ddao.Update();

                                    Application appreg = dao.GetDevLastGame(Dev.Id);
                                    try
                                    {
                                        foreach (var g in genres)
                                        {
                                            if (g.IsChecked == true)
                                            {
                                                ApplicationGenre ag = new ApplicationGenre();
                                                ag.ApplicationId = appreg.Id;
                                                ag.GenreId       = g.Id;
                                                agdao.Add(ag);
                                            }
                                        }
                                    }
                                    catch
                                    {
                                        AGError = true;
                                    }

                                    try
                                    {
                                        //Images
                                        int count = 0;
                                        foreach (var img in images)
                                        {
                                            string filePath = Guid.NewGuid() + Path.GetExtension(img.FileName);
                                            img.SaveAs(Path.Combine(Server.MapPath("~/media/app"), filePath));
                                            Image i = new Image();
                                            i.Url           = "../../../media/app/" + filePath;
                                            i.UserId        = int.Parse(Session["Id"].ToString());
                                            i.ApplicationId = appreg.Id;
                                            idao.Add(i);
                                            if (count == 0)
                                            {
                                                appreg.ImageUrl = i.Url;
                                                count++;
                                                dao.Update();
                                            }
                                        }
                                        //
                                    }
                                    catch
                                    {
                                        appreg.ImageUrl = "../../../assets/images/game-kingdoms-of-amalur-reckoning-4-500x375.jpg";
                                        dao.Update();
                                        ImgError = true;
                                        result   = result + "Error on Uploading Images, try later";
                                    }
                                    try
                                    {
                                        //File
                                        string filePath2 = Guid.NewGuid() + Path.GetExtension(File.FileName);
                                        if (!Directory.Exists(Server.MapPath("~/apps/appfiles/" + appreg.Id)))
                                        {
                                            Directory.CreateDirectory(Server.MapPath("~/apps/appfiles/" + appreg.Id));
                                        }
                                        File.SaveAs(Path.Combine(Server.MapPath("~/apps/appfiles/" + appreg.Id), filePath2));
                                        appreg.Archive = "../../../apps/appfiles/" + appreg.Id + "/" + filePath2;
                                        dao.Update();
                                        //
                                    }
                                    catch
                                    {
                                        FileError = true;
                                        result    = result + "Error on Uploading Files, try later";
                                    }

                                    if (ImgError || FileError || AGError)
                                    {
                                        return(Json(result, JsonRequestBehavior.AllowGet));
                                    }


                                    result = "Successfully Registered";
                                    return(Json(result, JsonRequestBehavior.AllowGet));
                                    //return RedirectToAction("Register");
                                }
                                else
                                {
                                    result = "There is already a game with this name"; return(Json(result, JsonRequestBehavior.AllowGet));
                                }
                            }
                            else
                            {
                                result = "Application Price is not acceptable"; return(Json(result, JsonRequestBehavior.AllowGet));
                            }
                        }
                        else
                        {
                            result = "The release date is not acceptable"; return(Json(result, JsonRequestBehavior.AllowGet));
                        }
                    }
                    else
                    {
                        result = "You are not a Developer"; return(Json(result, JsonRequestBehavior.AllowGet));
                    }
                }
                catch
                {
                    ViewBag.App = app;
                    result      = "An Error Occurred";
                    return(Json(result, JsonRequestBehavior.AllowGet));
                    //return RedirectToAction("Register");
                }
            }
            ViewBag.App   = app;
            ViewBag.Class = "alert alert-danger";
            result        = "An Error Occurred";
            return(Json(result, JsonRequestBehavior.AllowGet));
            //return View("Register");
        }