Skip to content

Commit

Permalink
Merge pull request #12 from scogliera/PokusAPI
Browse files Browse the repository at this point in the history
API - v1.0
  • Loading branch information
scogliera committed Dec 23, 2023
2 parents adbf09a + 3ea8ce3 commit e1f26d3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 24 deletions.
110 changes: 86 additions & 24 deletions Controllers/TdaApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public TdaApiController(ILogger<TdaApiController> logger, IMongoDal mongoDal)
}

[HttpGet("/api")]
public ActionResult<Dictionary<string, string>> OnGet()
public ActionResult<Dictionary<string, string>> OnBaseApiCall()
{
_logger.LogDebug("Base api endpoint called");
_logger.LogDebug("[GET] Base /api called");

var result = new Dictionary<string, string>
{
Expand All @@ -30,35 +30,97 @@ public ActionResult<Dictionary<string, string>> OnGet()
return Ok(result);
}

[HttpPost("/api")]
public ActionResult<Dictionary<string, string>> OnPost()
[HttpPost("/lecturers")]
public ActionResult<string> AddLecturer([FromBody] Lecturer lecturer)
{
_logger.LogDebug("Base api post endpoint called");
_logger.LogDebug("[POST] AddLecturer called");

var result = new Dictionary<string, string>
try
{
{ "secret", "The cake is a lie" }
};
_mongoDal.AddLecturer(lecturer);
return Ok("Lecturer přidán");
}
catch (Exception ex)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);
return Problem(statusCode: 500);
}
}

return Ok(result);
[HttpGet("/lecturers")]
public ActionResult<IEnumerable<Lecturer>> GetAllLecturers()
{
_logger.LogDebug("[GET] GetAllLecturers called");

try
{
var lecturers = _mongoDal.GetAllLecturers();
return Ok(lecturers);
}
catch (Exception ex)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);
return Problem(statusCode: 500);
}
}

[HttpPost("/prikladvseho/{vek:int}")]
public ActionResult<IEnumerable<string>> PrikladVseho([FromRoute] int vek, [FromBody] Lecturer lecturer)
[HttpGet("/lecturers/{uuid}")]
public ActionResult<Lecturer> GetLecturerByUuid([FromRoute] string uuid)
{
var anotherLecturer = new Lecturer
_logger.LogDebug("[GET] GetLecturerByUuid called");

try
{
FirstName = "Jozef",
LastName = "Novák",
TitleBefore = "Ing."
};

var informationList = new List<string>();

informationList.Add($"{lecturer.FirstName}{vek} let.");
informationList.Add($"Jeho ID je {lecturer.Uuid}");
informationList.Add($"{lecturer.FirstName} má kamaráda {anotherLecturer.FirstName} {anotherLecturer.LastName}");

return Ok(informationList);
var lecturer = _mongoDal.GetLecturer(new Guid(uuid));
if (lecturer == null)
return NotFound("Lecturer nenalezen - UUID");

return Ok(lecturer);
}
catch (Exception ex)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);
return Problem(statusCode: 500);
}
}

[HttpPut("/lecturers/{uuid}")]
public ActionResult<string> AddLecturerByUuid([FromBody] Lecturer lecturerNew, [FromRoute] string uuid)
{
_logger.LogDebug("[PUT] AddLecturerByUuid called");

try
{
_mongoDal.SetLecturer(lecturerNew, new Guid(uuid));
return Ok("Lecturer nastaven, pokud existoval");
}
catch(Exception ex)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);

//TODO: Odlišovat error u nás od nenalezena (Problem x NotFound)
//return Problem(statusCode: 500);
return NotFound("UUID nenalezeno");
}
}

[HttpDelete("/lecturers/{uuid}")]
public ActionResult<string> DeleteLecturerByUuid([FromRoute] string uuid)
{
_logger.LogDebug("[DELETE] DeleteLecturerByUuid called");

try
{
var success = _mongoDal.DeleteLecturer(new Guid(uuid));
if (!success)
return NotFound("Lecturer nenalezen - UUID");

return NoContent();
}
catch (Exception ex)
{
_logger.LogError("Message: {message}, StackTrace: {stackTrace}", ex.Message, ex.StackTrace);
return Problem(statusCode: 500);
}
}
}
1 change: 1 addition & 0 deletions DAL/MongoDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void SetLecturer(Lecturer lecturer, Guid uuid)

public bool DeleteLecturer(Guid id)
{
// TODO: Refactor return value - distinguish NotFound, Error and Success
var deleteResult = _lecturersCollection.DeleteOne(lecturer => lecturer.Uuid == id);
return deleteResult.DeletedCount > 0 && deleteResult.IsAcknowledged;
}
Expand Down

0 comments on commit e1f26d3

Please sign in to comment.