Exemple #1
0
        public IHttpActionResult Update(uint id, [FromBody] UpdateUserForm form)
        {
            Dictionary <string, string> errorList;
            uint currentUserId = Convert.ToUInt32(RequestContext.Principal.Identity.Name);

            if (!ModelState.IsValid)
            {
                errorList = AppUtils.Validation.GetErrorDictionary(ModelState);
                return(Content(
                           HttpStatusCode.BadRequest,
                           new ResponseWrapper <object>(HttpStatusCode.BadRequest, errorList)
                           ));
            }
            User user = new User();

            if (!user.DAL_Load(id))
            {
                return(NotFound());
            }

            user.Username     = form.Username;
            user.IsActive     = form.IsActive;
            user.FullName     = form.FullName;
            user.EmailAddress = form.EmailAddress;
            user.Designation  = form.Designation;
            user.MobileNumber = form.MobileNumber;
            user.ModifiedBy   = currentUserId;

            List <UserRole> userRoles             = UserRole.DAL_Load(id, null, null, null, null);
            List <uint>     willRetainUserRoleIds = userRoles
                                                    .FindAll(item => form.Roles.Exists(nItem => nItem == item.RoleId))
                                                    .ConvertAll(item => item.UserRoleId);
            List <uint> willAddRoleIds = form.Roles.FindAll(item => !userRoles.Exists(nItem => nItem.RoleId == item));

            var conn  = AppSetting.Db.OpenConnection();
            var trans = conn.BeginTransaction();

            try
            {
                if (user.DAL_Update(conn, trans) &&
                    UserRole.DAL_DeleteByUserId(conn, trans, user.UserId, string.Join(",", willRetainUserRoleIds)))
                {
                    bool hasErrorTransaction = willAddRoleIds.Exists(
                        item => !new UserRole
                    {
                        UserId    = user.UserId,
                        RoleId    = item,
                        CreatedBy = currentUserId,
                    }.DAL_AddExisting(conn, trans)
                        );

                    if (!hasErrorTransaction)
                    {
                        trans.Commit();
                        return(Ok(new ResponseWrapper <bool>(HttpStatusCode.OK, true)));
                    }
                }
                trans.Rollback();
            }
            catch (Exception)
            {
                trans.Rollback();
            }
            finally
            {
                AppSetting.Db.CloseConnection(ref conn);
            }

            ModelState.AddModelError("", "Failed to process your request");
            errorList = AppUtils.Validation.GetErrorDictionary(ModelState);
            return(Content(
                       HttpStatusCode.BadRequest,
                       new ResponseWrapper <object>(HttpStatusCode.BadRequest, errorList)
                       ));
        }