Skip to content

Commit

Permalink
Add more Swagger docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MegamindAme committed Sep 26, 2023
1 parent 373b373 commit 334526d
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 11 deletions.
Binary file modified .vs/ProjectEvaluation/webapplication4.metadata.v7.bin
Binary file not shown.
Binary file modified .vs/ProjectEvaluation/webapplication4.projects.v7.bin
Binary file not shown.
Binary file modified .vs/WebApplication4/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified .vs/WebApplication4/v17/.futdcache.v2
Binary file not shown.
Binary file modified .vs/WebApplication4/v17/.suo
Binary file not shown.
15 changes: 15 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Api App using ASP.NET 6 Web Api.
## Dependencies
1. .Net Framework 6


## Deployment
Host the Api to a server of your choice.
Run migrations using the nuget package manager console.
Run:

update-database

## Using the Api
The root url of the app will lead you to the Swagger documentation where details of all endpoints could be found there.

### Authentication
Use the register endpoint to register a user, and you'll get back a token which you can use to authenticate by using the Authorization header in your requests.
Once registered, you can login to get another token if your session expires.

## Running Tests
The test classes can be found in the Tests folder inside the project folder. Tests use Xunit, so you should have that package installed.

32 changes: 21 additions & 11 deletions WebApplication4/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
Expand All @@ -21,6 +22,14 @@ public LoginController(TestApiContext context)
_context = context;
}

/// <summary>
/// Login
/// </summary>
/// <remarks>Login to get a JWT token. Requires a username and a password to Login. Once authorized a token will be returned in the format {"token"}.</remarks>
/// <response code="200">Successfull</response>
/// <response code="200">Successfull</response>
/// <response code="4**">Request not well formed/Unsuccessful login</response>
/// <response code="500">Sorry, Error on our side</response>
[HttpPost, Route("login")]
public IActionResult Login(User loginDTO)
{
Expand All @@ -47,8 +56,8 @@ public IActionResult Login(User loginDTO)
expires: DateTime.Now.AddMinutes(10),
signingCredentials: signinCredentials
);
return Ok(new JwtSecurityTokenHandler().
WriteToken(jwtSecurityToken));
return Ok(new object[] {new JwtSecurityTokenHandler().
WriteToken(jwtSecurityToken)});
}
}
catch
Expand All @@ -58,7 +67,15 @@ public IActionResult Login(User loginDTO)
}
return Unauthorized();
}


/// <summary>
/// Register a new user
/// </summary>
/// <remarks>Register a new user. Requires a username, Email and password to register.Once registered a token will be returned in the format {"token"}.</remarks>
/// <response code="200">Successfull</response>
/// <response code="200">Successfull</response>
/// <response code="4**">Request not well formed/Unsuccessful login</response>
/// <response code="500">Sorry, Error on our side</response>
[HttpPost, Route("register")]
public async Task<IActionResult> Register(User user)
{
Expand All @@ -78,8 +95,6 @@ public async Task<IActionResult> Register(User user)
return Conflict();
}

//user.ID = (int) new Random().Next(0, 100000000);

_context.Users.Add(user);

try
Expand All @@ -94,8 +109,6 @@ public async Task<IActionResult> Register(User user)

try
{


var secretKey = new SymmetricSecurityKey
(Encoding.UTF8.GetBytes("thisisasecretkeyanditissupposedtowork"));
var signinCredentials = new SigningCredentials
Expand All @@ -111,16 +124,13 @@ public async Task<IActionResult> Register(User user)
token = new JwtSecurityTokenHandler().
WriteToken(jwtSecurityToken);

Ok("Token: " + token);
return Ok(new object[] { token });
}
catch
{
return BadRequest
("An error occurred in generating the token");
}

return Ok("Token:" + token);

}

private bool UserExists(int id)
Expand Down
38 changes: 38 additions & 0 deletions WebApplication4/Controllers/TasksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public TasksController(TestApiContext context)
_context = context;
}

/// <summary>
/// Get Tasks
/// </summary>
/// <remarks>Get Tasks List</remarks>
/// <response code="200">Successfull</response>
/// <response code="401">Not Authorized</response>
/// <response code="500">Sorry, Error on our side</response>
// GET: api/Tasks
[HttpGet]
public async Task<ActionResult<IEnumerable<Models.Task>>> GetTasks()
Expand All @@ -37,6 +44,13 @@ public TasksController(TestApiContext context)
return Ok(tasks);
}

/// <summary>
/// Get a Task
/// </summary>
/// <remarks>Get a Task by Id</remarks>
/// <response code="200">Successfull</response>
/// <response code="401">Not Authorized</response>
/// <response code="500">Sorry, Error on our side</response>
// GET: api/Tasks/5
[HttpGet("{id}")]
public async Task<ActionResult<Models.Task>> GetTask(int id)
Expand All @@ -55,6 +69,14 @@ public TasksController(TestApiContext context)
return Ok(task);
}

/// <summary>
/// Edit a Task
/// </summary>
/// <remarks>Edit a Task</remarks>
/// <response code="200">Successfull</response>
/// <response code="204">Successfully Edited</response>
/// <response code="401">Not Authorized</response>
/// <response code="500">Sorry, Error on our side</response>
// PUT: api/Tasks/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
Expand Down Expand Up @@ -86,6 +108,14 @@ public async Task<IActionResult> PutTask(int id, Models.Task task)
return NoContent();
}

/// <summary>
/// Add a Task
/// </summary>
/// <remarks>Add a Task</remarks>
/// <response code="200">Successfull</response>
/// <response code="201">Successfully Created</response>
/// <response code="401">Not Authorized</response>
/// <response code="500">Sorry, Error on our side</response>
// POST: api/Tasks
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
Expand Down Expand Up @@ -115,6 +145,14 @@ public async Task<IActionResult> PutTask(int id, Models.Task task)
return CreatedAtAction("GetTask", new { id = task.ID }, task);
}

/// <summary>
/// Delete a Task
/// </summary>
/// <remarks>Delete a Task by Id</remarks>
/// <response code="200">Successfull</response>
/// <response code="204">Successfully Deleted</response>
/// <response code="401">Not Authorized</response>
/// <response code="500">Sorry, Error on our side</response>
// DELETE: api/Tasks/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteTask(int id)
Expand Down
Binary file modified WebApplication4/bin/Debug/net6.0/WebApplication4.dll
Binary file not shown.
Binary file modified WebApplication4/bin/Debug/net6.0/WebApplication4.pdb
Binary file not shown.
68 changes: 68 additions & 0 deletions WebApplication4/bin/Debug/net6.0/WebApplication4.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified WebApplication4/obj/Debug/net6.0/WebApplication4.dll
Binary file not shown.
Binary file modified WebApplication4/obj/Debug/net6.0/WebApplication4.pdb
Binary file not shown.
68 changes: 68 additions & 0 deletions WebApplication4/obj/Debug/net6.0/WebApplication4.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 334526d

Please sign in to comment.