Esempio n. 1
0
        public async Task <ActionResult <Entry> > CreateEntry(EntryCreateDto entry)
        {
            int userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));

            Entry newEntry = new Entry
            {
                UserId   = userId,
                Date     = entry.Date,
                Summary  = entry.Summary,
                Activity = entry.Activity
            };

            _context.Entries.Add(newEntry);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetEntry", new { id = newEntry.Id }, newEntry));
        }
        public async Task <IActionResult> CreateEntryAsync(EntryCreateDto data)
        {
            if (data == null)
            {
                return(BadRequest());
            }

            var entryEntity = _mapper.Map <Entry>(data);

            entryEntity.UserId = this.GetCurrentUserId();

            // Add virtue links
            if (data.VirtueLinks?.Count > 0)
            {
                if (!await VirtueLinksAreValid(data.VirtueLinks))
                {
                    return(StatusCode(422, "Virtue link ID(s) must be valid and unique."));
                }

                entryEntity.VirtueLinks = await LinkVirtuesToEntryAsync(entryEntity, data.VirtueLinks);
            }

            await _unitOfWork.Entries.AddAsync(entryEntity);

            if (!await _unitOfWork.Complete())
            {
                throw new Exception("Creating an entry failed on save");
            }

            var entryToReturn = _mapper.Map <EntryGetDto>(entryEntity);

            return(CreatedAtRoute(
                       "GetEntry",
                       new { entryId = entryToReturn.EntryId },
                       entryToReturn
                       ));
        }