Exemple #1
0
    public string deleteWorkerWithId(string id)
    {
        Workers  wm = mongoDbase.getWorkerById(ObjectId.Parse(id));
        WorkersR w  = raven.getWorkerByEmail(wm.Email);

        if (w != null && wm != null)
        {
            if (w.Friends != null && wm.Friends != null)
            {
                for (int i = 0; i < w.Friends.Count; i++)
                {
                    var         temp = raven.getWorkerById(w.Friends[i]);
                    List <Guid> tpr  = new List <Guid>();
                    for (int j = 0; j < temp.Friends.Count; j++)
                    {
                        if (Guid.Parse(id) != temp.Friends[j])
                        {
                            tpr.Add(temp.Friends[j]);
                        }
                    }
                    temp.Friends = tpr;
                    raven.updateWorker(temp);
                }

                //mongo
                for (int i = 0; i < wm.Friends.Count; i++)
                {
                    var             temp = mongoDbase.getWorkerById(wm.Friends[i]);
                    List <ObjectId> tpr  = new List <ObjectId>();
                    for (int j = 0; j < temp.Friends.Count; j++)
                    {
                        if (ObjectId.Parse(id) != temp.Friends[j])
                        {
                            tpr.Add(temp.Friends[j]);
                        }
                    }
                    temp.Friends = tpr;
                    mongoDbase.updateWorker(temp);
                }
            }

            if (w.CompanyName != null && wm.CompanyName != null)
            {
                CompaniesR  c   = raven.getCompanyById(Guid.Parse(w.CompanyId));
                List <Guid> tpr = new List <Guid>();
                for (int j = 0; j < c.Employees.Count; j++)
                {
                    if (Guid.Parse(id) != c.Employees[j])
                    {
                        tpr.Add(c.Employees[j]);
                    }
                }
                c.Employees = tpr;
                raven.updateCompany(c);

                //mongo
                Companies       cm  = mongoDbase.getCompanyById(ObjectId.Parse(wm.CompanyId));
                List <ObjectId> tpm = new List <ObjectId>();
                for (int j = 0; j < cm.Employees.Count; j++)
                {
                    if (ObjectId.Parse(id) != cm.Employees[j])
                    {
                        tpm.Add(cm.Employees[j]);
                    }
                }
                cm.Employees = tpm;
                mongoDbase.updateCompany(cm);
            }
        }

        var res  = raven.deleteWorker(w);
        var resm = mongoDbase.removeWorker(wm.Id);

        var resdb = raven.deleteDBprefEntry(w.Id.ToString(), w.Email, w.Password);

        Changes ch = new Changes()
        {
            Actor1           = w.Id,
            Actor1Name       = w.FirstName + ' ' + w.LastName,
            Actor1Collection = "WorkersR",
            Type             = " has deleted profile from the network!",
            Time             = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
        };

        Changes changeFinal = raven.addFriendChange(ch);

        if (res != null && resm != null && changeFinal != null)
        {
            return("Worker deleted!");
        }
        else
        {
            return("Worker not found!");
        }
    }