public async Task <IActionResult> UpdateAuthor(int id, AuthorPutDTO authorToBeUpdated)
        {
            // Check that model is valid, and also that id in URI matches id in Body.
            if (!ModelState.IsValid ||
                id != authorToBeUpdated.AuthorId)
            {
                return(BadRequest(ModelState));
            }

            // Check that an author by that ID exists.
            Author existingAuthor = await _db.Authors
                                    .Where(a => a.AuthorId == id)
                                    .FirstOrDefaultAsync();

            if (existingAuthor == null)
            {
                return(NotFound());
            }

            // Check if the edited author already exists.
            Author existingAuthorWithSameName = await _db.Authors
                                                .Where(a =>
                                                       a.Praenomen == authorToBeUpdated.Praenomen &&
                                                       a.Nomen == authorToBeUpdated.Nomen &&
                                                       a.Cognomen == authorToBeUpdated.Cognomen)
                                                .FirstOrDefaultAsync();

            if (existingAuthorWithSameName != null)
            {
                return(Conflict(new { message = _errorMessageAuthorExists }));
            }

            try
            {
                int userId = (int)_userCtx.GetId();

                // First update author.
                _db.Entry(existingAuthor).CurrentValues.SetValues(authorToBeUpdated);
                existingAuthor.LastEditedBy   = userId;
                existingAuthor.LastEditedDate = DateTime.Now;
                await _db.SaveChangesAsync();

                // Prepare response.
                AuthorGetDTO editedAuthorResponse = AuthorGetDTO.FromModel(existingAuthor);

                // Trigger webhook for edited author event.
                TriggerAuthorWebhook(Event.EditedAuthor, editedAuthorResponse, userId);

                return(Ok(editedAuthorResponse));
            }
            catch (DbUpdateException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageSavingData));
            }
        }
 public async Task <IActionResult> GetAuthorsAddedByUser(int id)
 {
     try
     {
         return(Ok(await _db.Authors
                   .Where(a => a.AddedBy == id)
                   .OrderBy(a => a.Praenomen)
                   .Select(a => AuthorGetDTO.FromModel(a))
                   .ToListAsync()));
     }
     catch (InvalidOperationException)
     {
         return(StatusCode(StatusCodes.Status500InternalServerError,
                           _errorMessageFetchingData));
     }
 }
        public async Task <IActionResult> CreateAuthor(AuthorPostDTO authorToBeCreated)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Check if author already exists.
            Author existingAuthor = await _db.Authors.FirstOrDefaultAsync(a =>
                                                                          a.Praenomen == authorToBeCreated.Praenomen &&
                                                                          a.Nomen == authorToBeCreated.Nomen &&
                                                                          a.Cognomen == authorToBeCreated.Cognomen);

            if (existingAuthor != null)
            {
                return(Conflict(new { message = _errorMessageAuthorExists }));
            }

            try
            {
                int    userId    = (int)_userCtx.GetId();
                Author newAuthor = AuthorPostDTO.ToModel(authorToBeCreated, userId);

                await _db.Authors.AddAsync(newAuthor);

                await _db.SaveChangesAsync();

                string locationUri = $"{_baseUrl}/api/v1/authors/{newAuthor.AuthorId}";

                // Prepare response.
                AuthorGetDTO newAuthorResponse = AuthorGetDTO.FromModel(newAuthor);

                // Trigger webhook for new author event.
                TriggerAuthorWebhook(Event.NewAuthor, newAuthorResponse, userId);

                return(Created(locationUri, AuthorGetDTO.FromModel(newAuthor)));
            }
            catch (DbUpdateException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageSavingData));
            }
        }
        public async Task <IActionResult> GetAuthors(string period)
        {
            try
            {
                IEnumerable <AuthorGetDTO> existingAuthors = await _db.Authors
                                                             .OrderBy(a => a.Praenomen)
                                                             .Select(a => AuthorGetDTO.FromModel(a))
                                                             .ToListAsync();

                if (!String.IsNullOrEmpty(period))
                {
                    existingAuthors = existingAuthors.Where(a => a.Period.ToLower() == period.ToLower());
                }

                return(Ok(existingAuthors));
            }
            catch (InvalidOperationException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageFetchingData));
            }
        }
        public async Task <IActionResult> GetAuthor(int id)
        {
            Author existingAuthor = await _db.Authors
                                    .Where(a => a.AuthorId == id)
                                    .FirstOrDefaultAsync();

            if (existingAuthor == null)
            {
                return(NotFound());
            }

            try
            {
                AuthorGetDTO authorResponseDto = AuthorGetDTO.FromModel(existingAuthor);

                return(Ok(existingAuthor));
            }
            catch (InvalidOperationException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  _errorMessageFetchingData));
            }
        }