public async Task CreatePost([FromBody] PostForCreateDto postForCreateDto) { var post = new Post(); post.CreatedOn = postForCreateDto.CreatedOn; post.UpdatedOn = postForCreateDto.CreatedOn; post.Title = postForCreateDto.Title; post.Summary = postForCreateDto.Summary; post.Likes = 0; post.Comments = new List <Comment>(); post.Author = postForCreateDto.Author; if (!string.IsNullOrWhiteSpace(postForCreateDto.Pic)) { try { var path = await _fileStorageService.SaveFile(Convert.FromBase64String(postForCreateDto.Pic), "png", "posts"); post.Pic = path; } catch (Exception ex) { throw new Exception(ex.Message, ex); } } await _dbContext.AddAsync <Post>(post); await _dbContext.SaveChangesAsync(); }
public async Task <ActionResult <UserToken> > CreateUser([FromBody] UserInfo userInfo) { var user = new IdentityUser { UserName = userInfo.Email, Email = userInfo.Email }; var result = await _userManager.CreateAsync(user, userInfo.Password); if (result.Succeeded) { var userProfile = new UserProfile(); userProfile.Username = userInfo.Email; userProfile.ProfilePic = "https://u.cubeupload.com/adsuri/7NQDHz.jpeg"; try { _dbContext.Users.Add(userProfile); await _dbContext.SaveChangesAsync(); } catch { return(BadRequest("Failed to create user profile")); } return(BuildToken(userInfo)); } else { return(BadRequest("Username or password is invalid")); } }
public async Task <IActionResult> GetUserProfile([FromBody] UserProfileForUpdateDto profileForUpdate, [FromRoute] string Username) { var existingProfile = await _dbContext.Users.FirstOrDefaultAsync((user) => user.Username == Username); existingProfile.FirstName = profileForUpdate.FirstName; existingProfile.LastName = profileForUpdate.LastName; existingProfile.ProfilePic = profileForUpdate.ProfilePic; existingProfile.Status = profileForUpdate.Status; existingProfile.DOB = profileForUpdate.DOB; existingProfile.Intro = profileForUpdate.Intro; _dbContext.Users.Update(existingProfile); await _dbContext.SaveChangesAsync(); return(Ok(existingProfile)); }