Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create endpoints for Persons, Sources and City. Move private info to env file. #32

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.txt
.env
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

appsettings.Development.json
appsettings.json
*.txt
efbundle

# dotenv files
Expand Down
108 changes: 108 additions & 0 deletions backend/Controllers/CityController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Microsoft.AspNetCore.Mvc;
using Mappa.Services;
using Mappa.Dtos;
using Mappa.Entities;
using Microsoft.AspNetCore.Authorization;

namespace Mappa.Controllers;

[ApiController]
[Route("[controller]")]
public class CityController : ControllerBase
{
private readonly IComplexEntityService<City, CityGeneralDto, CityDetailDto,
CityCreateRequest, CityUpdateRequest>
_service;

public CityController(IComplexEntityService<City, CityGeneralDto,
CityDetailDto, CityCreateRequest, CityUpdateRequest> service)
{
_service = service;
}

[HttpGet]
public async Task<IActionResult> GetAll()
{
var items = await _service.GetAllAsync();
return Ok(items);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
try
{
var item = await _service.GetByIdAsync(id);
return Ok(item);
}
catch (ArgumentException ex) when (ex.Message.Contains($"Entity with ID {id} not found."))
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CityCreateRequest request)
{
try
{
var item = await _service.CreateAsync(request);
return CreatedAtAction(nameof(GetById), new { id = item.Id }, item);
}
catch (ArgumentException ex)
{
return Conflict(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, [FromBody] CityUpdateRequest request)
{
try
{
var item = await _service.UpdateAsync(id, request);
return Ok(item);
}
catch (ArgumentException ex)
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
try
{
var result = await _service.DeleteAsync(id);
if (!result)
return NotFound(new {Message = $"Item with {id} not found"});

return NoContent();
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

}
9 changes: 4 additions & 5 deletions backend/Controllers/EntityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public async Task<IActionResult> Delete(int id)
{
try
{
await _service.DeleteAsync(id);
var result = await _service.DeleteAsync(id);
if (!result)
return NotFound(new {Message = $"Item with ${id} not found"});

return NoContent();
}
catch (ArgumentException ex) when (ex.Message.Contains($"Entity with ID {id} not found."))
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
Expand Down
108 changes: 108 additions & 0 deletions backend/Controllers/OrdinaryPersonController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Microsoft.AspNetCore.Mvc;
using Mappa.Services;
using Mappa.Dtos;
using Mappa.Entities;
using Microsoft.AspNetCore.Authorization;

namespace Mappa.Controllers;

[ApiController]
[Route("[controller]")]
public class OrdinaryPersonController : ControllerBase
{
private readonly IComplexEntityService<OrdinaryPerson, OrdinaryPersonGeneralDto,
OrdinaryPersonDetailDto, OrdinaryPersonCreateRequest, OrdinaryPersonUpdateRequest>
_service;

public OrdinaryPersonController(IComplexEntityService<OrdinaryPerson, OrdinaryPersonGeneralDto,
OrdinaryPersonDetailDto, OrdinaryPersonCreateRequest, OrdinaryPersonUpdateRequest> service)
{
_service = service;
}

[HttpGet]
public async Task<IActionResult> GetAll()
{
var items = await _service.GetAllAsync();
return Ok(items);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
try
{
var item = await _service.GetByIdAsync(id);
return Ok(item);
}
catch (ArgumentException ex) when (ex.Message.Contains($"Entity with ID {id} not found."))
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPost]
public async Task<IActionResult> Create([FromBody] OrdinaryPersonCreateRequest request)
{
try
{
var item = await _service.CreateAsync(request);
return CreatedAtAction(nameof(GetById), new { id = item.Id }, item);
}
catch (ArgumentException ex)
{
return Conflict(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, [FromBody] OrdinaryPersonUpdateRequest request)
{
try
{
var item = await _service.UpdateAsync(id, request);
return Ok(item);
}
catch (ArgumentException ex)
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
try
{
var result = await _service.DeleteAsync(id);
if (!result)
return NotFound(new {Message = $"Item with {id} not found"});

return NoContent();
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

}
107 changes: 107 additions & 0 deletions backend/Controllers/SecondarySourceController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Microsoft.AspNetCore.Mvc;
using Mappa.Services;
using Mappa.Dtos;
using Mappa.Entities;
using Microsoft.AspNetCore.Authorization;

namespace Mappa.Controllers;

[ApiController]
[Route("[controller]")]
public class SecondarySourceController : ControllerBase
{
private readonly IComplexEntityService<SecondarySource, SecondarySourceGeneralDto, SecondarySourceDetailDto, SecondarySourceCreateRequest, SecondarySourceUpdateRequest>
_service;

public SecondarySourceController(IComplexEntityService<SecondarySource, SecondarySourceGeneralDto,
SecondarySourceDetailDto, SecondarySourceCreateRequest, SecondarySourceUpdateRequest> service)
{
_service = service;
}

[HttpGet]
public async Task<IActionResult> GetAll()
{
var items = await _service.GetAllAsync();
return Ok(items);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
try
{
var item = await _service.GetByIdAsync(id);
return Ok(item);
}
catch (ArgumentException ex) when (ex.Message.Contains($"Entity with ID {id} not found."))
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPost]
public async Task<IActionResult> Create([FromBody] SecondarySourceCreateRequest request)
{
try
{
var item = await _service.CreateAsync(request);
return CreatedAtAction(nameof(GetById), new { id = item.Id }, item);
}
catch (ArgumentException ex)
{
return Conflict(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, [FromBody] SecondarySourceUpdateRequest request)
{
try
{
var item = await _service.UpdateAsync(id, request);
return Ok(item);
}
catch (ArgumentException ex)
{
return NotFound(new { Message = ex.Message });
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

[Authorize]
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
try
{
var result = await _service.DeleteAsync(id);
if (!result)
return NotFound(new {Message = $"Item with {id} not found"});

return NoContent();
}
catch (Exception ex)
{
// Log the exception (optional)
return StatusCode(500, new { Message = "An unexpected error occurred.", Details = ex.Message });
}
}

}
Loading