|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using PaymentCoreServiceApi.Services; |
| 4 | + |
| 5 | +namespace PaymentCoreServiceApi.Controllers; |
| 6 | + |
| 7 | +[ApiController] |
| 8 | +[Route("api/[controller]")] |
| 9 | +[Authorize] |
| 10 | +public class ProjectController : ControllerBase |
| 11 | +{ |
| 12 | + private readonly ICurrentUser _currentUser; |
| 13 | + |
| 14 | + public ProjectController(ICurrentUser currentUser) |
| 15 | + { |
| 16 | + _currentUser = currentUser; |
| 17 | + } |
| 18 | + |
| 19 | + [HttpGet("my-projects")] |
| 20 | + public IActionResult GetUserProjects() |
| 21 | + { |
| 22 | + // Simulate getting projects for the current user |
| 23 | + var userProjects = new[] |
| 24 | + { |
| 25 | + new { |
| 26 | + Id = 1, |
| 27 | + Name = "Project A", |
| 28 | + OwnerId = _currentUser.Id, |
| 29 | + OwnerName = _currentUser.UserName, |
| 30 | + CreatedAt = DateTime.UtcNow.AddDays(-5) |
| 31 | + }, |
| 32 | + new { |
| 33 | + Id = 2, |
| 34 | + Name = "Project B", |
| 35 | + OwnerId = _currentUser.Id, |
| 36 | + OwnerName = _currentUser.UserName, |
| 37 | + CreatedAt = DateTime.UtcNow.AddDays(-2) |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + return Ok(new |
| 42 | + { |
| 43 | + UserId = _currentUser.Id, |
| 44 | + UserEmail = _currentUser.Email, |
| 45 | + Projects = userProjects |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + [HttpGet("{projectId}")] |
| 50 | + public IActionResult GetProjectById(int projectId) |
| 51 | + { |
| 52 | + // Simulate getting a specific project with validation |
| 53 | + if (!_currentUser.IsAuthenticated) |
| 54 | + { |
| 55 | + return Unauthorized(); |
| 56 | + } |
| 57 | + |
| 58 | + // Simulate project data (in real app, this would come from a database) |
| 59 | + var project = new |
| 60 | + { |
| 61 | + Id = projectId, |
| 62 | + Name = $"Project {projectId}", |
| 63 | + Description = "Project description here", |
| 64 | + OwnerId = _currentUser.Id, |
| 65 | + OwnerName = _currentUser.UserName, |
| 66 | + OwnerEmail = _currentUser.Email, |
| 67 | + CreatedAt = DateTime.UtcNow, |
| 68 | + Status = "Active", |
| 69 | + Members = new[] |
| 70 | + { |
| 71 | + new { Id = _currentUser.Id, Name = _currentUser.UserName, Role = "Owner" } |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return Ok(project); |
| 76 | + } |
| 77 | + |
| 78 | + [HttpPost("create")] |
| 79 | + public IActionResult CreateProject([FromBody] CreateProjectRequest request) |
| 80 | + { |
| 81 | + if (!ModelState.IsValid) |
| 82 | + { |
| 83 | + return BadRequest(ModelState); |
| 84 | + } |
| 85 | + |
| 86 | + // Simulate creating a new project |
| 87 | + var newProject = new |
| 88 | + { |
| 89 | + Id = new Random().Next(100, 999), // Simulate generated ID |
| 90 | + Name = request.Name, |
| 91 | + Description = request.Description, |
| 92 | + OwnerId = _currentUser.Id, |
| 93 | + OwnerName = _currentUser.UserName, |
| 94 | + CreatedAt = DateTime.UtcNow, |
| 95 | + Status = "Active" |
| 96 | + }; |
| 97 | + |
| 98 | + return CreatedAtAction(nameof(GetProjectById), new { projectId = newProject.Id }, newProject); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +public class CreateProjectRequest |
| 103 | +{ |
| 104 | + public string Name { get; set; } = string.Empty; |
| 105 | + public string Description { get; set; } = string.Empty; |
| 106 | +} |
0 commit comments