public async Task <IActionResult> CreateAsync([FromBody] Client.Entry.EntryBuildInfo entryBuildInfo, CancellationToken cancellationToken) { if (entryBuildInfo == null) { var error = ServiceErrorResponses.BodyIsMissing("Entry"); return(this.BadRequest(error)); } var userId = User.FindFirstValue("userId"); if (userId == null) { var error = ServiceErrorResponses.InvalidClaims("userId"); return(this.BadRequest(error)); } var entryCreationInfo = new EntryCreationInfo(userId, entryBuildInfo.ActivityId); Entry modelEntry; try { modelEntry = await entryRepository.CreateAsync(entryCreationInfo, cancellationToken); } catch (EntryDuplicationException e) { return(BadRequest(e.Message)); } var clientEntry = EntryConverter.Convert(modelEntry); return(CreatedAtRoute( "GetEntry", new { id = clientEntry.Id }, clientEntry)); }
public Task <Entry> CreateAsync(EntryCreationInfo entryCreationInfo, CancellationToken cancellationToken) { if (entryCreationInfo == null) { throw new ArgumentNullException(nameof(entryCreationInfo)); } cancellationToken.ThrowIfCancellationRequested(); var countSameEntries = entries.Find(ent => ent.UserId == entryCreationInfo.UserId && ent.ActivityId == entryCreationInfo.ActivityId).CountDocuments(); if (countSameEntries > 0) { throw new EntryDuplicationException(entryCreationInfo.UserId, entryCreationInfo.ActivityId); } var entry = new Entry { Status = entryCreationInfo.Status, UserId = entryCreationInfo.UserId, CreatedAt = DateTime.Now, ActivityId = entryCreationInfo.ActivityId }; entries.InsertOne(entry, cancellationToken: cancellationToken); return(Task.FromResult(entry)); }