Beispiel #1
0
    public string updateWorkerInDb(string id, string mail, string pass, string name, string last, string company, string previous, string skills, string dbch)
    {
        Workers  recvm = mongoDbase.getWorkerById(ObjectId.Parse(id));
        WorkersR recvv = raven.getWorkerByEmail(recvm.Email);

        if (previous == "")
        {
            previous = null;
        }

        if (skills == "")
        {
            skills = null;
        }

        recvv.Email     = recvm.Email = mail;
        recvv.Password  = recvm.Password = pass;
        recvv.FirstName = recvm.FirstName = name;
        recvv.LastName  = recvm.LastName = last;

        if (previous != null)
        {
            List <PrevEmp>  pre   = new List <PrevEmp>();
            List <PrevEmpM> prem  = new List <PrevEmpM>();
            JArray          tempp = JArray.Parse(previous);
            if (tempp != null)
            {
                for (int i = 0; i < tempp.Count; i++)
                {
                    PrevEmp           emp   = new PrevEmp();
                    JToken            token = tempp[i];
                    string            sol   = (string)token["firm"];
                    List <CompaniesR> comp  = raven.getCompanyByName(sol);
                    if (comp != null)
                    {
                        emp.FirmName         = comp[0].CompanyName;
                        emp.FirmId           = comp[0].Id;
                        emp.FormerEmployeeId = recvv.Id;
                        emp.StartTime        = (string)token["dates"];
                        emp.EndTime          = (string)token["datee"];
                    }

                    if (emp != null)
                    {
                        pre.Add(emp);
                    }
                    else
                    {
                        return(fail);
                    }
                }

                for (int i = 0; i < tempp.Count; i++)
                {
                    PrevEmpM         empm  = new PrevEmpM();
                    JToken           token = tempp[i];
                    string           sol   = (string)token["firm"];
                    List <Companies> compm = mongoDbase.getCompanyByName(sol);
                    if (compm != null)
                    {
                        empm.FirmName         = compm[0].CompanyName;
                        empm.FirmId           = compm[0].Id;
                        empm.FormerEmployeeId = recvm.Id;
                        empm.StartTime        = (string)token["dates"];
                        empm.EndTime          = (string)token["datee"];
                    }

                    if (empm != null)
                    {
                        prem.Add(empm);
                    }
                    else
                    {
                        return(fail);
                    }
                }
            }
            else
            {
                return(fail);
            }
            recvv.PreviousEmployment = pre;
            recvm.PreviousEmployment = prem;
        }
        else
        {
            recvv.PreviousEmployment = null;
            recvm.PreviousEmployment = null;
        }


        if (skills != null)
        {
            List <string> ski    = new List <string>();
            JArray        tempps = JArray.Parse(skills);
            if (tempps != null)
            {
                for (int i = 0; i < tempps.Count; i++)
                {
                    JToken token = tempps[i];
                    string sol   = (string)token;

                    if (sol != null)
                    {
                        ski.Add(sol);
                    }
                    else
                    {
                        return(fail);
                    }
                }
            }
            else
            {
                return(fail);
            }
            recvv.Skills = ski;
            recvm.Skills = ski;
        }
        else
        {
            recvv.Skills = null;
            recvm.Skills = null;
        }


        var temp = recvv.CompanyName;

        var cId  = raven.getCompanyByName(company);
        var cmId = mongoDbase.getCompanyByName(company);

        Changes changeFinal = null;

        if (cId.Count == 0 || cmId.Count == 0)
        {
            return("There is no such company!");
        }

        if (temp != company && temp != null)
        {
            var tempC = raven.getCompanyByName(temp);
            var ret   = raven.removeWorkerFromCompany(recvv.Id, tempC[0]);
            var com   = raven.addWorkerToCompany(recvv.Id, cId[0]);
            recvv.CompanyId   = cId[0].Id.ToString();
            recvv.CompanyName = cId[0].CompanyName;
        }
        else if (temp == null)
        {
            var com = raven.addWorkerToCompany(recvv.Id, cId[0]);
            recvv.CompanyId   = cId[0].Id.ToString();
            recvv.CompanyName = cId[0].CompanyName;
        }

        var res = raven.updateWorker(recvv);

        //mongo
        if (temp != company && temp != null)
        {
            var tempC = mongoDbase.getCompanyByName(temp);
            var ret   = mongoDbase.removeWorkerFromCompany(recvm.Id, tempC[0]);
            var com   = mongoDbase.addWorkerToCompany(recvm.Id, cmId[0]);
            recvm.CompanyId   = cmId[0].Id.ToString();
            recvm.CompanyName = cmId[0].CompanyName;
        }
        else if (temp == null)
        {
            var com = mongoDbase.addWorkerToCompany(recvm.Id, cmId[0]);
            recvm.CompanyId   = cmId[0].Id.ToString();
            recvm.CompanyName = cmId[0].CompanyName;
        }

        var resm = mongoDbase.updateWorker(recvm);

        if (temp != company || temp == null)
        {
            Changes ch = new Changes()
            {
                Actor1           = res.Id,
                Actor1Collection = "WorkersR",
                Actor1Name       = res.FirstName + ' ' + res.LastName,
                Type             = " has added as employer ",
                Actor2           = Guid.Parse(res.CompanyId),
                Actor2Name       = res.CompanyName,
                Actor2Collection = "CompaniesR",
                Time             = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };

            changeFinal = raven.addFriendChange(ch);
        }
        else
        {
            Changes ch = new Changes()
            {
                Actor1           = res.Id,
                Actor1Name       = res.FirstName + ' ' + res.LastName,
                Actor1Collection = "WorkersR",
                Type             = " has updated profile!",
                Time             = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            };

            changeFinal = raven.addFriendChange(ch);
        }

        var dbTemplist = raven.getDBPref();

        string dbtemp = null;

        if (dbTemplist.Count != 0)
        {
            for (int i = 0; i < dbTemplist.Count; i++)
            {
                if (dbTemplist[i].MongoId == recvm.Id.ToString() && dbTemplist[i].RavenId == recvv.Id.ToString())
                {
                    dbtemp = dbTemplist[i].DbName;
                }
            }
            if (dbtemp == null)
            {
                DBCheck dbc = new DBCheck()
                {
                    Collection = "worker",
                    DbName     = "raven",
                    Mail       = mail,
                    Password   = pass,
                    MongoId    = recvm.Id.ToString(),
                    RavenId    = recvv.Id.ToString()
                };

                var dbcRet = raven.setDB(dbc);
            }
        }

        if (dbtemp != null && dbtemp != dbch)
        {
            DBCheck dbc = new DBCheck()
            {
                Collection = "worker",
                DbName     = dbch,
                Mail       = mail,
                Password   = pass,
                MongoId    = recvm.Id.ToString(),
                RavenId    = recvv.Id.ToString()
            };

            var dbcRet = raven.setDB(dbc);
        }

        if (res != null && resm != null && changeFinal != null)
        {
            if (dbch == "raven")
            {
                HttpContext.Current.Session.Add("userR", res);
                HttpContext.Current.Session.Add("user", null);
                HttpContext.Current.Session.Add("database", "raven");
                return("Update successfull!");
            }
            else if (dbch == "mongo")
            {
                HttpContext.Current.Session.Clear();
                HttpContext.Current.Session.Add("user", resm);
                HttpContext.Current.Session.Add("userR", null);
                HttpContext.Current.Session.Add("database", "mongo");
                return("Update successfull!");
            }
        }
        return(fail);
    }