/// <summary> /// Verify user name when edit(exclude own data) /// </summary> /// <param name="dto"></param> /// <param name="userEmail"></param> /// <returns></returns> public ApiResult <EditMemberDTO> VerifyEmailAndNameWhenEdit(EditMemberDTO dto, string userEmail) { //Exclude own data IQueryable <ForumMembers> source = _members.GetAll().Where(x => x.Email != userEmail && x.Name == dto.Name); if (source.Any(x => x.Name == dto.Name)) { return(new ApiResult <EditMemberDTO>($"{dto.Name} already exists,choose another one!")); } else { return(new ApiResult <EditMemberDTO>()); } }
public ApiResult <EditMemberDTO> UpdateMember([FromBody] EditMemberDTO dto) { if (ModelState.IsValid) { ApiResult <EditMemberDTO> result = _userService.VerifyEmailAndNameWhenEdit(dto, User.Identity.Name); if (result.Issuccessful) { _userService.Edit(dto, User.Identity.Name); } return(result); } else { return(new ApiResult <EditMemberDTO>("id is null")); } }
/// <summary> /// Edit user info /// </summary> /// <param name="dto"></param> /// <param name="userEmail"></param> public void Edit(EditMemberDTO dto, string userEmail) { try { ForumMembers editedContext = _members.GetFirst(x => x.Email == userEmail); editedContext.Name = dto.Name; editedContext.Password = _encrypt.ToMD5(dto.Password); editedContext.Age = dto.Age; editedContext.Phone = dto.Phone; editedContext.Gender = dto.Gender; editedContext.ImgLink = dto.ImgLink; //Edit TitleId base on input TitleName editedContext.TitleId = _titles.GetFirst(x => x.TitleName == dto.TitleName).TitleId; _members.Update(editedContext); _members.SaveContext(); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }