コード例 #1
0
        public ResponseVM Delete(Guid id)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        ITGroupMember itgroupmemberTobeDeleted = context.ITGroupMembers.Find(id);
                        if (itgroupmemberTobeDeleted == null)
                        {
                            return(new ResponseVM("deleted", false, "ITGroupMember", ResponseVM.DOES_NOT_EXIST));
                        }
                        //delete

                        var removeFromGroupEmployees = context.GroupEmployees.Where(x => x.ITGroupMemberid == itgroupmemberTobeDeleted.ITGroupMemberid);
                        context.GroupEmployees.RemoveRange(removeFromGroupEmployees);
                        context.SaveChanges();

                        context.ITGroupMembers.Remove(itgroupmemberTobeDeleted);
                        context.SaveChanges();

                        dbTransaction.Commit();
                        return(new ResponseVM
                                   ("deleted", true, "ITGroupMember"));
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        return(new ResponseVM
                                   ("deleted", false, "ITGroupMember", ResponseVM.SOMETHING_WENT_WRONG, "", ex));
                    }
                }
            }
        }
コード例 #2
0
        public ResponseVM Update(ITGroupMemberVM itgroupmemberVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        ITGroupMember itgroupmemberTobeUpdated = context.ITGroupMembers.Where(x => x.ITGroupMemberid == Guid.Parse(itgroupmemberVM.ITGroupMemberid)).FirstOrDefault();

                        if (itgroupmemberTobeUpdated == null)
                        {
                            return(new ResponseVM("update", false, "ITGroupMember", ResponseVM.DOES_NOT_EXIST));
                        }

                        itgroupmemberTobeUpdated.ITGroupid = Guid.Parse(itgroupmemberVM.ITGroupid);
                        context.SaveChanges();

                        var removeFromGroupEmployees = context.GroupEmployees.Where(x => x.ITGroupMemberid == itgroupmemberTobeUpdated.ITGroupMemberid);
                        context.GroupEmployees.RemoveRange(removeFromGroupEmployees);
                        context.SaveChanges();


                        foreach (var empID in itgroupmemberVM.EmployeeIdList)
                        {
                            var employee = context.Employees.Find(empID);
                            if (employee == null)
                            {
                                return(new ResponseVM("create", false, "ITGroupMember", "Employee does not exists"));
                            }
                            var groupEmployee = new GroupEmployee
                            {
                                EmployeeID       = empID,
                                ITGroupMemberid  = itgroupmemberTobeUpdated.ITGroupMemberid,
                                EmployeeFullName = toViewModel.ToFullName(employee.FirstName, employee.LastName)
                            };

                            context.GroupEmployees.Add(groupEmployee);
                            context.SaveChanges();
                        }

                        dbTransaction.Commit();
                        return(new ResponseVM("updated", true, "ITGroupMember"));
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        return(new ResponseVM
                                   ("updated", false, "ITGroupMember", ResponseVM.SOMETHING_WENT_WRONG, "", ex));

                        throw;
                    }
                }
            }
        }