示例#1
0
        public GroupDTO UpdateGroupinDB(GroupDTO groupDTO)
        {
            BaseDataAccess _db = new BaseDataAccess();

            using (DbTransaction dbTransaction = _db.CreateTransaction())
            {
                try
                {
                    // 1- Perform Mapping to  Input (for Saving in DB)
                    if (Mapper.MapGroupAsInput(groupDTO))
                    {
                        // 2- Select Group to be updated
                        GroupBusiness groupBusiness = new GroupBusiness();
                        GroupList     groupList     = groupBusiness.SelectRows(Mapper._Group.Id, null);

                        if (groupList != null && groupList.Count > 0)
                        {
                            groupList[0].name   = Mapper._Group.name;
                            groupList[0].status = Mapper._Group.status;

                            // 3- Update Group Data by Input Values
                            groupBusiness = new GroupBusiness();
                            if (groupBusiness.UpdateRow(dbTransaction, groupList[0]) > 0)
                            {
                                // 4- Remove Group Functions Already Saved for that Group in DB
                                GroupFunctionBusiness groupFunctionBusiness = new GroupFunctionBusiness();
                                GroupFunctionList     groupFunctionList     = groupFunctionBusiness.SelectRows(null, null, Mapper._Group.Id);

                                if (groupFunctionList != null && groupFunctionList.Count > 0)
                                {
                                    foreach (GroupFunction groupFunction in groupFunctionList)
                                    {
                                        groupFunctionBusiness = new GroupFunctionBusiness();
                                        groupFunctionBusiness.DeleteRow(dbTransaction, groupFunction);
                                    }
                                }

                                // 5- Add New Group Functions from Input
                                if (Mapper._GroupFunctionListInput != null && Mapper._GroupFunctionListInput.Count > 0)
                                {
                                    foreach (GroupFunction groupFunction in Mapper._GroupFunctionListInput)
                                    {
                                        groupFunctionBusiness = new GroupFunctionBusiness();
                                        groupFunctionBusiness.InsertRow(dbTransaction, groupFunction);
                                    }

                                    dbTransaction.Commit();
                                }
                            }
                            else
                            {
                                dbTransaction.Rollback();
                                throw new Exception("DataBase Operation Failure");
                            }
                        }
                        else
                        {
                            throw new Exception("Group Id Not Found in DB");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("groupDTO");
                    }
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw new Exception("DataBase Operation Failure");
                }
            }

            return(groupDTO);
        }
示例#2
0
        public GroupDTO AddGrouptoDB(GroupDTO groupDTO)
        {
            BaseDataAccess _db = new BaseDataAccess();

            using (DbTransaction dbTransaction = _db.CreateTransaction())
            {
                try
                {
                    // 1- Perform Mapping to  Input (for Saving in DB)
                    if (Mapper.MapGroupAsInput(groupDTO))
                    {
                        ////User user = Mapper._User;
                        ////List<UserGroup> userGroups = Mapper._UserGroupListInput;
                        //Mapper._User.createDate = DateTime.Now;

                        // 2- Insert Group in DB
                        GroupBusiness groupBusiness = new GroupBusiness();
                        if (groupBusiness.InsertRow(dbTransaction, Mapper._Group) > 0)
                        {
                            groupDTO.Id = Mapper._Group.Id;

                            if (Mapper._GroupFunctionListInput != null && Mapper._GroupFunctionListInput.Count > 0)
                            {
                                GroupFunctionBusiness groupFunctionBusiness = new GroupFunctionBusiness();

                                // 3- Insert Group Functions in DB
                                foreach (GroupFunction groupFunction in Mapper._GroupFunctionListInput)
                                {
                                    groupFunction.groupId = Mapper._Group.Id;

                                    groupFunctionBusiness = new GroupFunctionBusiness();
                                    groupFunctionBusiness.InsertRow(dbTransaction, groupFunction);
                                }

                                dbTransaction.Commit();
                            }
                            else
                            {
                                dbTransaction.Rollback();
                                throw new Exception("DataBase Operation Failure");
                            }
                        }
                        else
                        {
                            throw new Exception("Group Id Not Found in DB");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("groupDTO");
                    }
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw new Exception("DataBase Operation Failure");
                }
            }

            return(groupDTO);
        }