Exemplo n.º 1
0
        public void Request_Create()
        {
            //---------------------------------------------

            var request = ProfileRequest.Create("TEST");

            Assert.Equal("TEST", request.Command);
            Assert.Empty(request.Args);
            Assert.Equal("TEST:", request.ToString());

            //---------------------------------------------

            var args = new Dictionary <string, string>();

            request = ProfileRequest.Create("TEST", args);

            Assert.Equal("TEST", request.Command);
            Assert.Empty(request.Args);
            Assert.Equal("TEST:", request.ToString());

            //---------------------------------------------

            args["arg1"] = "1";

            request = ProfileRequest.Create("TEST", args);

            Assert.Equal("TEST", request.Command);
            Assert.Single(request.Args);
            Assert.Equal("1", request.Args["arg1"]);
            Assert.Equal("TEST: arg1=1", request.ToString());

            //---------------------------------------------

            args["arg2"] = "2";

            request = ProfileRequest.Create("TEST", args);

            Assert.Equal("TEST", request.Command);
            Assert.Equal(2, request.Args.Count);
            Assert.Equal("1", request.Args["arg1"]);
            Assert.Equal("2", request.Args["arg2"]);
            Assert.Equal("TEST: arg1=1, arg2=2", request.ToString());
        }
Exemplo n.º 2
0
        public ContentResult AccountUpdate(FormCollection form, HttpPostedFileBase file)
        {
            JObject json = new JObject();

            json["error"]   = false;
            json["message"] = "";

            if (this.IsLoggedIn())
            {
                string[] keys = new string[]
                {
                    "password", "email",
                    "hyear", "hmonth", "hday", "firstname", "lastname", "status", "bmonth", "bday", "byear",
                    "contact", "emergency-name", "emergency-number", "emergency-name", "emergency-rel", "house", "city",
                    "province", "street", "department", "sex", "education", "history"
                };

                if (this.HasValues(form, keys))
                {
                    try
                    {
                        Account ac = this.GetAccount();
                        Profile p  = new Profile();
                        ac.Password = form.GetValue("password").AttemptedValue;
                        ac.Email    = form.GetValue("email").AttemptedValue;

                        if (file != null && file.ContentLength > 0)
                        {
                            var filename = Path.GetFileName(file.FileName);
                            var name     = filename.Substring(0, filename.LastIndexOf('.'));
                            var ext      = filename.Substring(filename.LastIndexOf('.'));
                            name    += "-" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm");
                            filename = name + ext;

                            var path = Path.Combine(HttpContext.Server.MapPath("~/Content/img/uploads"), filename);
                            file.SaveAs(path);
                            ac.Image = Path.Combine("~/Content/img/uploads", filename);
                        }

                        p.FirstName = form.GetValue("firstname").AttemptedValue;
                        if (form.GetValue("middlename") != null)
                        {
                            p.MiddleName = form.GetValue("middlename").AttemptedValue;
                        }
                        else
                        {
                            p.MiddleName = "";
                        }

                        p.LastName    = form.GetValue("lastname").AttemptedValue;
                        p.Sex         = (SexType)Int32.Parse(form.GetValue("sex").AttemptedValue);
                        p.CivilStatus =
                            (CivilStatusType)Int32.Parse(form.GetValue("status").AttemptedValue);

                        p.BirthDate = DateTime.ParseExact(
                            form.GetValue("byear").AttemptedValue + "-"
                            + (Int32.Parse(
                                   form.GetValue("bmonth").AttemptedValue) + 1)
                            .ToString("00") + "-"
                            + Int32.Parse(form.GetValue("bday").AttemptedValue)
                            .ToString("00"),
                            "yyyy-MM-dd",
                            CultureInfo.InvariantCulture);

                        p.Contact       = form.GetValue("contact").AttemptedValue;
                        p.ContactPerson = form.GetValue("emergency-name").AttemptedValue;
                        p.CPersonNo     = form.GetValue("emergency-number").AttemptedValue;
                        p.CPersonRel    = form.GetValue("emergency-rel").AttemptedValue;
                        p.HouseNo       = form.GetValue("house").AttemptedValue;
                        p.City          = form.GetValue("city").AttemptedValue;
                        p.Province      = form.GetValue("province").AttemptedValue;
                        p.Street        = form.GetValue("street").AttemptedValue;

                        JObject education = JObject.Parse(form.GetValue("education").AttemptedValue);
                        JArray  history   = JArray.Parse(form.GetValue("history").AttemptedValue);

                        Education edu           = new Education(instantiate: false);
                        bool      hasProperties = false;

                        foreach (JProperty property in education.Properties())
                        {
                            if (property.Name == "elementary")
                            {
                                edu.Elementary         = new EducationLevel(EducationType.Elementary);
                                edu.Elementary.Name    = property.Value["name"].ToString();
                                edu.Elementary.Address = property.Value["address"].ToString();
                                edu.Elementary.Start   = property.Value["start"].ToString();
                                edu.Elementary.End     = property.Value["end"].ToString();
                                hasProperties          = true;
                            }
                            else if (property.Name == "hs")
                            {
                                edu.HighSchool         = new EducationLevel(EducationType.HighSchool);
                                edu.HighSchool.Name    = property.Value["name"].ToString();
                                edu.HighSchool.Address = property.Value["address"].ToString();
                                edu.HighSchool.Start   = property.Value["start"].ToString();
                                edu.HighSchool.End     = property.Value["end"].ToString();
                                hasProperties          = true;
                            }
                            else if (property.Name == "college")
                            {
                                edu.College         = new EducationLevel(EducationType.College);
                                edu.College.Name    = property.Value["name"].ToString();
                                edu.College.Address = property.Value["address"].ToString();
                                edu.College.Start   = property.Value["start"].ToString();
                                edu.College.End     = property.Value["end"].ToString();
                                hasProperties       = true;
                            }
                            else if (property.Name == "post")
                            {
                                edu.PostGraduate         = new EducationLevel(EducationType.PostGraduate);
                                edu.PostGraduate.Name    = property.Value["name"].ToString();
                                edu.PostGraduate.Address = property.Value["address"].ToString();
                                edu.PostGraduate.Start   = property.Value["start"].ToString();
                                edu.PostGraduate.End     = property.Value["end"].ToString();
                                hasProperties            = true;
                            }
                        }

                        List <EmploymentHistory> hist = new List <EmploymentHistory>();

                        foreach (JObject o in history)
                        {
                            EmploymentHistory temp = new EmploymentHistory();
                            temp.Address       = o.GetValue("address").ToString();
                            temp.CompanyName   = o.GetValue("company").ToString();
                            temp.Position      = o.GetValue("position").ToString();
                            temp.ContactName   = o.GetValue("cperson").ToString();
                            temp.ContactNo     = o.GetValue("number").ToString();
                            temp.StartDate     = o.GetValue("start").ToString();
                            temp.EndDate       = o.GetValue("end").ToString();
                            temp.LeavingReason = o.GetValue("leave").ToString();
                            hist.Add(temp);
                        }

                        if (hasProperties == true)
                        {
                            p.Education = edu;
                        }
                        else
                        {
                            p.Education = null;
                        }

                        ac.Update(false);
                        p.Create();

                        foreach (EmploymentHistory o in hist)
                        {
                            o.Create(p.ProfileID);
                        }

                        ProfileRequest req = new ProfileRequest();
                        req.CurrentProfile = ac.Profile.Profile;
                        req.NewProfile     = p;
                        req.Create();

                        json["message"] = "Succesfully requested for a profile update, please wait for the HR to approve of your update.";
                    }
                    catch (Exception e)
                    {
                        json["error"]   = true;
                        json["message"] = e.Message;
                    }
                }
                else
                {
                    json["error"]   = true;
                    json["message"] = "Form is incomplete";
                }
            }
            else
            {
                json["error"]   = true;
                json["message"] = "You are not authorized to continue";
            }

            return(Content(json.ToString(), "application/json"));
        }