using AutoMapper; using AutoMapper.QueryableExtensions; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using OnlyPrompt.Backend.ApiModels.UserProfile; using OnlyPrompt.Backend.Database; using OnlyPrompt.Backend.Utils; namespace OnlyPrompt.Backend.Controllers { [ApiController] [Route("api/v1/profiles")] [Authorize(Roles = ModelConstants.UserRole)] public class ProfileController : BaseController { public ProfileController(OnlyPromptContext db, IMapper mapper) : base(db, mapper) { } [HttpGet("{id}")] public async Task, Ok>> GetProfileAsync(Identifier id) { var userId = User.GetUserId(); var profile = await _db.UserProfiles.OfIdentifer(id) .Where(up => up.IsPublic || up.Id == userId) .ProjectTo(_mapper.ConfigurationProvider) .FirstOrDefaultAsync(); if (profile is null) return TypedResults.NotFound("Profile not found or is private."); return TypedResults.Ok(profile); } } }