Example #1
0
        public ResponseVM Delete(Guid id)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        EmployeeEmployeeType employeeEmployeeTypeTobeDeleted = context.EmployeeEmployeeTypes.Find(id);
                        if (employeeEmployeeTypeTobeDeleted == null)
                        {
                            return(new ResponseVM("deleted", false, "EmployeeEmployeeType", ResponseVM.DOES_NOT_EXIST));
                        }
                        //delete

                        var removeFromTypeEmployees = context.TypeEmployees.Where(x => x.EmployeeEmployeeTypeid == employeeEmployeeTypeTobeDeleted.EmployeeEmployeeTypeid);
                        context.TypeEmployees.RemoveRange(removeFromTypeEmployees);
                        context.SaveChanges();

                        context.EmployeeEmployeeTypes.Remove(employeeEmployeeTypeTobeDeleted);
                        context.SaveChanges();

                        dbTransaction.Commit();
                        return(new ResponseVM
                                   ("deleted", true, "EmployeeEmployeeType"));
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        return(new ResponseVM
                                   ("deleted", false, "EmployeeEmployeeType", ResponseVM.SOMETHING_WENT_WRONG, "", ex));
                    }
                }
            }
        }
Example #2
0
        public ResponseVM Update(EmployeeEmployeeTypeVM employeeEmployeeTypeVM)
        {
            using (context)
            {
                using (var dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        EmployeeEmployeeType employeeEmployeeTypeTobeUpdated = context.EmployeeEmployeeTypes.Where(x => x.EmployeeEmployeeTypeid == Guid.Parse(employeeEmployeeTypeVM.EmployeeEmployeeTypeid)).FirstOrDefault();


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

                        employeeEmployeeTypeTobeUpdated.EmployeeTypeid = Guid.Parse(employeeEmployeeTypeVM.EmployeeTypeid);
                        context.SaveChanges();

                        var removeFromTypeEmployees = context.TypeEmployees.Where(x => x.EmployeeEmployeeTypeid == employeeEmployeeTypeTobeUpdated.EmployeeEmployeeTypeid);
                        context.TypeEmployees.RemoveRange(removeFromTypeEmployees);
                        context.SaveChanges();


                        foreach (var empID in employeeEmployeeTypeVM.EmployeeIdList)
                        {
                            var employee = context.Employees.Find(empID);
                            if (employee == null)
                            {
                                return(new ResponseVM("create", false, "EmployeeEmployeeType", "Employee does not exists"));
                            }
                            var typeEmployee = new TypeEmployee
                            {
                                EmployeeID             = empID,
                                EmployeeEmployeeTypeid = employeeEmployeeTypeTobeUpdated.EmployeeEmployeeTypeid,
                                EmployeeFullName       = toViewModel.ToFullName(employee.FirstName, employee.LastName)
                            };

                            context.TypeEmployees.Add(typeEmployee);
                            context.SaveChanges();
                        }

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

                        throw;
                    }
                }
            }
        }