summaryrefslogtreecommitdiffstats
path: root/src/server/Api/V1/Entries/CreateEntryRoute.cs
blob: ebe49fc30b42dc493dc9293c3bfb2b07cd0126bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using IOL.BookmarkThing.Server.Api.V1.Entries.Dtos;

namespace IOL.BookmarkThing.Server.Api.V1.Entries;

public class CreateEntryRoute : RouteBaseV1Sync.WithRequest<CreateEntryRequest>.WithActionResult<EntryDto>
{
	private readonly AppDbContext _context;

	public CreateEntryRoute(AppDbContext context) {
		_context = context;
	}

	/// <summary>
	/// Create a new entry
	/// </summary>
	/// <param name="entry">The entry to create</param>
	/// <response code="200">Entry created successfully</response>
	/// <response code="400">Invalid entry</response>
	[ProducesResponseType(typeof(EntryDto), 200)]
	[ProducesResponseType(typeof(List<ErrorResult>), 400)]
	[ApiVersion(ApiSpecV1.VERSION_STRING)]
	[HttpPost("~/v{version:apiVersion}/entries/create")]
	public override ActionResult<EntryDto> Handle(CreateEntryRequest entry) {
		var errors = entry.GetErrors();
		if (errors.Count != 0) {
			return BadRequest(errors);
		}

		var dbEntry = entry.AsDbEntity(LoggedInUser.Id);
		_context.Entries.Add(dbEntry);
		_context.SaveChanges();
		return Ok(new EntryDto(dbEntry));
	}
}