using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using System.Security.Claims; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using MyRecipes2.Server.Data; using MyRecipes2.Server.Models; using MyRecipes2.Shared; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace MyRecipes2.Server.Controllers { [ApiController] public class PublicProfileController : ControllerBase { private readonly ApplicationDbContext dbContext; public PublicProfileController(ApplicationDbContext dbContext) { this.dbContext = dbContext; } [HttpGet] [Route("[controller]/Data/{userId}")] public UserDto Get(string userId) { try { var userData = dbContext.Users .Where(u => u.Id == userId) .Include(x => x.Image) .First(); UserDto result = new UserDto(); result.Id = userData.Id; result.Bio = userData.Bio; result.Email = userData.Email; result.Username = userData.UserName; result.ProfileImageId = userData.Image?.Id; if (result.ProfileImageId == null) { result.ProfileImageUrl = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNiIgaGVpZ2h0PSIxNiIgZmlsbD0iY3VycmVudENvbG9yIiBjbGFzcz0iYmkgYmktcGVyc29uLWNpcmNsZSIgdmlld0JveD0iMCAwIDE2IDE2Ij4KICA8cGF0aCBkPSJNMTEgNmEzIDMgMCAxIDEtNiAwIDMgMyAwIDAgMSA2IDB6Ii8+CiAgPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBkPSJNMCA4YTggOCAwIDEgMSAxNiAwQTggOCAwIDAgMSAwIDh6bTgtN2E3IDcgMCAwIDAtNS40NjggMTEuMzdDMy4yNDIgMTEuMjI2IDQuODA1IDEwIDggMTBzNC43NTcgMS4yMjUgNS40NjggMi4zN0E3IDcgMCAwIDAgOCAxeiIvPgo8L3N2Zz4="; } else { result.ProfileImageUrl = $"/Image/{result.ProfileImageId}"; } return result; } catch (InvalidOperationException e) { throw new ArgumentException("User Id does not exist"); } } [HttpPut] [Route("[controller]/Edit/{userId}")] public void Put(string userId, UserDto user) { try { ApplicationUser userData = dbContext.Users .Where(u => u.Id == userId) .First(); Console.WriteLine(user.ProfileImageId); userData.Bio = user.Bio; if (user.ProfileImageId != null) { userData.Image = dbContext.Images .Where(x => x.Id == user.ProfileImageId) .First(); } dbContext.SaveChanges(); } catch (InvalidOperationException e) { throw new ArgumentException("User Id does not exist"); } } } }