public string UpdateChatGroup(ChatGroup groupinfo)
        {
            try
            {
                UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
                if (groupinfo.GroupId == 0)
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    groupinfo.USERID       = loggedinUser.USERID;
                                    groupinfo.ModifiedById = loggedinUser.USERID.ToString();
                                    groupinfo.CreatedById  = loggedinUser.USERID.ToString();
                                    groupinfo.CreatedDate  = DateTime.Now;
                                    groupinfo.Status       = 1;
                                    // groupinfo.GroupType = 1;
                                    db.ChatGroups.Add(groupinfo);
                                    db.SaveChanges();
                                    if (groupinfo.GroupId > 0)
                                    {
                                        List <MyNode> responseNodes =
                                            JsonConvert.DeserializeObject <List <MyNode> >(groupinfo.UserSelections);

                                        foreach (MyNode @node in responseNodes)
                                        {
                                            ChatGroupParticipant participant = new ChatGroupParticipant
                                            {
                                                USERID       = @node.value,
                                                GroupId      = groupinfo.GroupId,
                                                CreatedById  = loggedinUser.USERID.ToString(),
                                                ModifiedById = loggedinUser.USERID.ToString(),
                                                CreatedDate  = DateTime.Now,
                                                ModifiedDate = DateTime.Now,
                                                IsActive     = true,
                                                IsAdmin      = false,
                                                Status       = 1
                                            };
                                            db.ChatGroupParticipants.Add(participant);
                                        }
                                        db.SaveChanges();
                                    }
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
                else
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    groupinfo = Updateobject(groupinfo.GroupId, groupinfo);
                                    groupinfo.ModifiedById    = loggedinUser.USERID.ToString();
                                    groupinfo.ModifiedDate    = DateTime.Now;
                                    db.Entry(groupinfo).State = EntityState.Modified;
                                    db.SaveChanges();

                                    List <MyNode> responseNodes =
                                        JsonConvert.DeserializeObject <List <MyNode> >(groupinfo.UserSelections);
                                    List <ChatGroupParticipant> participantsInDb =
                                        _context.ChatGroupParticipants.Where(x => x.GroupId == groupinfo.GroupId)
                                        .ToList();

                                    List <int> indb         = participantsInDb.Select(x => x.USERID).ToList();
                                    List <int> inselections = responseNodes.Select(x => x.value).ToList();

                                    var toAdd    = UserDatatablesController.ExcludedRight(indb, inselections);
                                    var toDelete = UserDatatablesController.ExcludedLeft(indb, inselections);

                                    foreach (int @id in toAdd)
                                    {
                                        db.ChatGroupParticipants.Add(new ChatGroupParticipant
                                        {
                                            USERID       = @id,
                                            GroupId      = groupinfo.GroupId,
                                            CreatedById  = loggedinUser.USERID.ToString(),
                                            ModifiedById = loggedinUser.USERID.ToString(),
                                            CreatedDate  = DateTime.Now,
                                            ModifiedDate = DateTime.Now,
                                            IsActive     = true,
                                            IsAdmin      = false,
                                            Status       = 1
                                        });
                                    }
                                    foreach (
                                        ChatGroupParticipant existingChatGroupParticipant in
                                        toDelete.Select(
                                            id =>
                                            db.ChatGroupParticipants.SingleOrDefault(
                                                x => x.USERID.Equals(id) && x.GroupId.Equals(groupinfo.GroupId)))
                                        )
                                    {
                                        db.ChatGroupParticipants.Remove(existingChatGroupParticipant);
                                    }
                                    db.SaveChanges();
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }
            return("SUCESS");
        }
Example #2
0
        public string UpdateFacilittMaster(FacilityMaster facilityinfo)
        {
            try
            {
                UserLoginInformation loggedinUser = (UserLoginInformation)LoginController.ActiveUser;
                if (facilityinfo.FacilityMasterId == 0)
                {
                    facilityinfo.CreatedById  = loggedinUser.USERID.ToString();
                    facilityinfo.ModifiedById = loggedinUser.USERID.ToString();
                    facilityinfo.CreatedDate  = DateTime.Now;
                    facilityinfo.ModifiedDate = DateTime.Now;
                    _context.FacilityMasters.Add(facilityinfo);
                    _context.SaveChanges();
                }
                else
                {
                    try
                    {
                        using (TransactionScope transactionScope = new TransactionScope())
                        {
                            try
                            {
                                using (kryptoEntities1 db = new kryptoEntities1())
                                {
                                    facilityinfo = Updateobject(facilityinfo.FacilityMasterId, facilityinfo);
                                    facilityinfo.ModifiedById    = loggedinUser.USERID.ToString();
                                    facilityinfo.ModifiedDate    = DateTime.Now;
                                    db.Entry(facilityinfo).State = EntityState.Modified;
                                    db.SaveChanges();

                                    List <MyNode> responseNodes =
                                        JsonConvert.DeserializeObject <List <MyNode> >(facilityinfo.UserSelections);
                                    List <UserLoginInformation> usersInDb = new List <UserLoginInformation>();

                                    usersInDb.AddRange(facilityinfo.GetAssocaitedFacilityAdmins());

                                    List <int> indb         = usersInDb.Select(x => x.USERID).ToList();
                                    List <int> inselections = responseNodes.Select(x => x.value).ToList();

                                    var toInclude = UserDatatablesController.ExcludedRight(indb, inselections);
                                    var toExclude = UserDatatablesController.ExcludedLeft(indb, inselections);

                                    foreach (int @in in toInclude)
                                    {
                                        UserLoginInformation current = db.UserLoginInformations.Find(@in);
                                        current.IsFacilityAdmin = true;
                                        db.Entry(current).State = EntityState.Modified;
                                    }
                                    foreach (int @out in toExclude)
                                    {
                                        UserLoginInformation current = db.UserLoginInformations.Find(@out);
                                        current.IsFacilityAdmin = false;
                                        db.Entry(current).State = EntityState.Modified;
                                    }
                                    db.SaveChanges();
                                }
                                transactionScope.Complete();
                            }
                            catch (Exception ee)
                            {
                                return("FAIL");
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        return("FAIL");
                    }
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                return("FAIL");
            }
            return("SUCESS");
        }