Skip to content

Commit 340ae1e

Browse files
committed
Added event endpoint to the open api
1 parent 2f30da1 commit 340ae1e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
namespace LearningHub.NHS.OpenAPI.Controllers
2+
{
3+
using System.Threading.Tasks;
4+
using LearningHub.Nhs.Models.Common;
5+
using LearningHub.Nhs.Models.Entities.Analytics;
6+
using LearningHub.Nhs.OpenApi.Services.Interface.Services;
7+
using Microsoft.AspNetCore.Authorization;
8+
using Microsoft.AspNetCore.Mvc;
9+
10+
/// <summary>
11+
/// Event operations.
12+
/// </summary>
13+
[Authorize(Policy = "AuthorizeOrCallFromLH")]
14+
[Route("Event")]
15+
[ApiController]
16+
public class EventController : OpenApiControllerBase
17+
{
18+
/// <summary>
19+
/// The event service.
20+
/// </summary>
21+
private readonly IEventService eventService;
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="EventController"/> class.
25+
/// </summary>
26+
/// <param name="eventService">The event service.</param>
27+
public EventController(
28+
IEventService eventService)
29+
{
30+
this.eventService = eventService;
31+
}
32+
33+
/// <summary>
34+
/// Get specific event by Id.
35+
/// </summary>
36+
/// <param name="id">The id.</param>
37+
/// <returns>The <see cref="Task"/>.</returns>
38+
[HttpGet("{id}")]
39+
public async Task<ActionResult> GetAsync(int id)
40+
{
41+
return this.Ok(await eventService.GetByIdAsync(id));
42+
}
43+
44+
/// <summary>
45+
/// The create event async.
46+
/// </summary>
47+
/// <param name="eventEntity">The event.</param>
48+
/// <returns>The <see cref="Task"/>.</returns>
49+
[HttpPost]
50+
[Route("Create")]
51+
public async Task<IActionResult> CreateAsync([FromBody] Event eventEntity)
52+
{
53+
eventEntity.UserId = !eventEntity.UserId.HasValue ? this.CurrentUserId : eventEntity.UserId;
54+
var vr = await eventService.CreateAsync(this.CurrentUserId.GetValueOrDefault(), eventEntity);
55+
56+
if (vr.IsValid)
57+
{
58+
return this.Ok(new ApiResponse(true, vr));
59+
}
60+
else
61+
{
62+
return this.BadRequest(new ApiResponse(false, vr));
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Update an existing Event.
68+
/// </summary>
69+
/// <param name="eventEntity">The event.</param>
70+
/// <returns>The <see cref="Task"/>.</returns>
71+
[HttpPut]
72+
public async Task<IActionResult> PutAsync([FromBody] Event eventEntity)
73+
{
74+
var vr = await eventService.UpdateAsync(this.CurrentUserId.GetValueOrDefault(), eventEntity);
75+
if (vr.IsValid)
76+
{
77+
return this.Ok(new ApiResponse(true, vr));
78+
}
79+
else
80+
{
81+
return this.BadRequest(new ApiResponse(false, vr));
82+
}
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)