コード例 #1
0
ファイル: UserDAL.cs プロジェクト: MohamedMedina/SamDb
        public UserDTO UpdateUserinDB(UserDTO userDTO)
        {
            BaseDataAccess _db = new BaseDataAccess();

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

                        //UserBusiness userBusiness = new UserBusiness();
                        //if (userBusiness.InsertRow(dbTransaction, Mapper._User) > 0)
                        //{
                        //    UserGroupBusiness userGroupBusiness = new UserGroupBusiness();

                        //    if (Mapper._UserGroupListInput != null && Mapper._UserGroupListInput.Count > 0)
                        //    {
                        //        foreach (UserGroup userGroup in Mapper._UserGroupListInput)
                        //        {
                        //            userGroup.userId = Mapper._User.Id;

                        //            userGroupBusiness = new UserGroupBusiness();
                        //            userGroupBusiness.InsertRow(dbTransaction, userGroup);
                        //        }

                        //        dbTransaction.Commit();
                        //    }
                        //    else
                        //        dbTransaction.Rollback();
                        //}
                        //else
                        //    throw new DataException("DataBase Operation Failure");

                        // 2- Select User to be updated
                        UserBusiness userBusiness = new UserBusiness();
                        UserList     userList     = userBusiness.SelectRows(Mapper._User.Id, null, null, null);

                        if (userList != null && userList.Count > 0)
                        {
                            userList[0].userName = Mapper._User.userName;
                            userList[0].fullName = Mapper._User.fullName;
                            userList[0].Password = Mapper._User.Password;
                            userList[0].status   = Mapper._User.status;

                            // 3- Update User Data by Input Values
                            userBusiness = new UserBusiness();
                            if (userBusiness.UpdateRow(dbTransaction, userList[0]) > 0)
                            {
                                // 4- Remove User Groups Already Saved for that User in DB
                                UserGroupBusiness userGroupBusiness = new UserGroupBusiness();
                                UserGroupList     userGroupList     = userGroupBusiness.SelectRows(null, Mapper._User.Id, null);

                                if (userGroupList != null && userGroupList.Count > 0)
                                {
                                    //foreach (UserGroup userGroup in Mapper._UserGroupList)
                                    //{
                                    //    userGroupBusiness = new UserGroupBusiness();
                                    //    userGroupBusiness.DeleteRow(dbTransaction, userGroup);
                                    //}

                                    foreach (UserGroup userGroup in userGroupList)
                                    {
                                        userGroupBusiness = new UserGroupBusiness();
                                        userGroupBusiness.DeleteRow(dbTransaction, userGroup);
                                    }
                                }

                                // 5- Add New User Groups from Input
                                if (Mapper._UserGroupListInput != null && Mapper._UserGroupListInput.Count > 0)
                                {
                                    foreach (UserGroup userGroup in Mapper._UserGroupListInput)
                                    {
                                        userGroupBusiness = new UserGroupBusiness();
                                        userGroupBusiness.InsertRow(dbTransaction, userGroup);
                                    }

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

            return(userDTO);
        }
コード例 #2
0
ファイル: UserDAL.cs プロジェクト: MohamedMedina/SamDb
        public UserDTO AddUsertoDB(UserDTO userDTO)
        {
            BaseDataAccess _db = new BaseDataAccess();

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

                        // 2- Insert User in DB
                        UserBusiness userBusiness = new UserBusiness();
                        if (userBusiness.InsertRow(dbTransaction, Mapper._User) > 0)
                        {
                            userDTO.Id = Mapper._User.Id;

                            if (Mapper._UserGroupListInput != null && Mapper._UserGroupListInput.Count > 0)
                            {
                                UserGroupBusiness userGroupBusiness = new UserGroupBusiness();

                                // 3- Insert User Groups in DB
                                foreach (UserGroup userGroup in Mapper._UserGroupListInput)
                                {
                                    userGroup.userId = Mapper._User.Id;

                                    userGroupBusiness = new UserGroupBusiness();
                                    userGroupBusiness.InsertRow(dbTransaction, userGroup);
                                }

                                dbTransaction.Commit();
                            }
                            else
                            {
                                dbTransaction.Rollback();
                                throw new Exception("DataBase Operation Failure");
                            }
                        }
                        else
                        {
                            throw new Exception("DataBase Operation Failure");
                        }
                    }
                    else
                    {
                        throw new ArgumentNullException("userDTO");
                    }
                }
                catch (Exception ex)
                {
                    dbTransaction.Rollback();
                    throw new Exception("DataBase Operation Failure");
                }
            }

            return(userDTO);
        }