private RoleViewModel ToRoleViewModel(Role role) { return new RoleViewModel { Id = role.Id, Name = role.Name, TimestampString = Convert.ToBase64String(role.Timestamp) }; }
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)); }