public IHttpActionResult PutRole(int id, RoleViewModel role_viewModel)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (id != role_viewModel.Id)
                return BadRequest();

            //把資料庫中的那筆資料讀出來
            var role_db = db.Roles.Find(id);
            if (role_db == null)
            {
                return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!"));
            }
            else
            {
                try
                {
                    role_db.Name = role_viewModel.Name;
                    db.Entry(role_db).OriginalValues["Timestamp"] = Convert.FromBase64String(role_viewModel.TimestampString);
                    db.SaveChanges();

                    //寫入AccessLog
                    MPAccessLog.WriteEntry(User.Identity.Name, AccessAction.Update, "Role", JsonConvert.SerializeObject(new { role_db.Id, role_db.Name }));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (db.Roles.Find(id) == null)
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!"));
                    else
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "這筆資料已被其他人修改!"));
                }
            }

            return Ok(ToRoleViewModel(role_db));
        }
        public void PostRole_ShouldAddARole()
        {
            // arrange
            var controller = new MembershipController();
            RoleViewModel roleTest = new RoleViewModel { Name = "測試角色" };

            // act
            var result = controller.PostRole(roleTest) as CreatedAtRouteNegotiatedContentResult<RoleViewModel>;

            // assert
            Assert.IsNotNull(result);
            Assert.AreEqual("GetRoleById", result.RouteName);
            Assert.IsTrue(Convert.ToInt32(result.RouteValues["id"]) > 0);
            Assert.AreEqual(roleTest.Name, result.Content.Name);
        }
        public IHttpActionResult PostRole(RoleViewModel role_viewModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            Role role = new Role { Id = role_viewModel.Id, Name = role_viewModel.Name };
            db.Roles.Add(role);
            try
            {
                db.SaveChanges();

                //寫入AccessLog
                MPAccessLog.WriteEntry(User.Identity.Name, AccessAction.Create, "Role", JsonConvert.SerializeObject(new {role.Id,role.Name }));
            }
            catch (DbEntityValidationException ex)
            {
                var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage);
                var getFullMessage = string.Join("; ", entityError);
                var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage);
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, exceptionMessage));
            }

            return CreatedAtRoute("GetRoleById", new { id = role.Id }, ToRoleViewModel(role));
        }
        public void UpdateRole_ShouldUpdateRoleName()
        {
            // arrange
            var controller = new MembershipController();
            var role = controller.GetRoles().Where(r => r.Name == "測試角色").Select(r=>r).First();
            RoleViewModel roleTest = new RoleViewModel { Id = role.Id, Name = "測試角色AAA", TimestampString=role.TimestampString };

            // act
            var result = controller.PutRole(roleTest.Id, roleTest) as OkNegotiatedContentResult<RoleViewModel>;

            // assert
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Content);
            Assert.IsTrue(result.Content.Id ==roleTest.Id);
            Assert.IsTrue(result.Content.Name == roleTest.Name);
        }