public async Task <IActionResult> Add([FromBody] AlbumGroup albumGroup)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    AlbumGroupDetail group = await _repository.AddAsync(albumGroup);

                    return(CreatedAtRoute(
                               new { controller = "album", action = nameof(Get), key = group.Key }, group));
                }
                catch (RepositoryException e)
                {
                    return(BadRequest(new ApiErrorRep(e.Message)));
                }
                catch (Exception e)
                {
                    _logger.LogError("AddAlbum", e, "Error adding album");
                    return(StatusCode(500, new ApiErrorRep("Unknown error")));
                }
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
        public async void AddAlbumGroup_ReturnsNonNullResult_WithValidInput()
        {
            AlbumGroup       input  = CreateValidCreateModel();
            AlbumGroupDetail result = await this.repo.AddAsync(input);

            Assert.NotNull(result);
        }
        public async Task <IActionResult> Get(string key)
        {
            try
            {
                AlbumGroupDetail group = await _repository.GetAsync(key);

                if (group != null)
                {
                    return(Ok(group));
                }
                else
                {
                    return(NotFound(new ApiErrorRep($"Album group with key {key} was nto found")));
                }
            }
            catch (RepositoryException e)
            {
                return(BadRequest(new ApiErrorRep(e.Message)));
            }
            catch (Exception e)
            {
                _logger.LogError("GetArtist", e, "Error getting album");
                return(StatusCode(500, new ApiErrorRep("Unknown error")));
            }
        }
        public async void AddAlbumGroup_ThrowsEntityAlreadyExistsRepositoryException_WhenAttemptingToCreateAnExistingGroupKey()
        {
            AlbumGroup       input  = CreateValidCreateModel();
            AlbumGroupDetail result = await this.repo.AddAsync(input);

            await Assert.ThrowsAsync <EntityAlreadyExistsRepositoryException>(() => this.repo.AddAsync(input));
        }
        public async void GetAlbumGroup_ReturnsNullWithInvalidKey()
        {
            int groupId = await SetupValidRecordInDatabase();

            AlbumGroupDetail result = await this.repo.GetAsync("non_valid_key_input_string");

            Assert.Null(result);
        }
        public async void GetAlbumGroup_ReturnsNullWithInvalidId()
        {
            int groupId = await SetupValidRecordInDatabase();

            AlbumGroupDetail result = await this.repo.GetAsync(56256757);

            Assert.Null(result);
        }
        public async void GetAlbumGroup_ReturnsCorrectResultWithValidKey()
        {
            int groupId = await SetupValidRecordInDatabase();

            AlbumGroupDetail result = await this.repo.GetAsync(VALID_GROUP_KEY);

            Assert.NotNull(result);
            Assert.IsType <AlbumGroupDetail>(result);
        }
        public async void UpdateAlbumGroup_ReturnsMatchingName_WithValidInput()
        {
            int groupId = await SetupValidRecordInDatabase();

            AlbumGroup input = CreateValidCreateModel();

            input.Name = "NewName";
            AlbumGroupDetail result = await this.repo.UpdateAsync(groupId, input);

            Assert.Equal("NewName", result.Name);
        }
        public async void UpdateAlbumGroup_ReturnsAlbumGroupDetailResult_WithValidInput()
        {
            int groupId = await SetupValidRecordInDatabase();

            AlbumGroup input = CreateValidCreateModel();

            input.Name = "NewName";
            AlbumGroupDetail result = await this.repo.UpdateAsync(groupId, input);

            Assert.IsType <AlbumGroupDetail>(result);
        }
 public AlbumGroupViewModel Map(AlbumGroupDetail a)
 {
     return(new AlbumGroupViewModel
     {
         Id = a.Id,
         Created = a.Created,
         Updated = a.Updated,
         Key = a.Key,
         Name = a.Name,
         TotalAlbums = a.TotalAlbums
     });
 }
        public async Task <IActionResult> Reorder(int id)
        {
            ReorderAlbumGroupViewModel model = new ReorderAlbumGroupViewModel();

            model.GroupId = id;
            AlbumGroupDetail group = await _albumGroupRepo.GetAsync(id);

            AlbumGroupItemPositionListResult itemsResult = await _albumGroupRepo.GetOrderedAlbumListAsync(id);

            model.GroupName = group.Name;
            model.Items     = itemsResult.Items.Select(x => new ReorderAlbumGroupItemViewModel
            {
                AlbumId       = x.AlbumId,
                AlbumTitle    = x.AlbumTitle,
                PositionIndex = x.PositionIndex
            }).ToList();
            return(View(model));
        }
        public async Task <IActionResult> Edit(EditAlbumGroupViewModel model)
        {
            AlbumGroupDetail group = await _albumGroupRepo.GetAsync(model.Id);

            if (group != null)
            {
                model.Key = group.Key; // key cant be overwritten
                if (ModelState.IsValid)
                {
                    try {
                        group.Name = model.Name;

                        await _albumGroupRepo.UpdateAsync(model.Id, group);

                        this.SetBootstrapPageAlert("Success", "Album group updated", BootstrapAlertType.success);
                        return(RedirectToAction(nameof(Index)));
                    }
                    catch (Exception e)
                    {
                        if (e is RepositoryException)
                        {
                            ModelState.AddModelError(nameof(model.Key), e.Message);
                        }
                        else
                        {
                            ModelState.AddModelError(nameof(model.Key), "Unknown error");
                            _log.LogError("EditAlbumGroup", e, "Error updating group");
                        }
                        return(View(model));
                    }
                }
                else
                {
                    return(View(model));
                }
            }
            else
            {
                // todo: show error message
                return(RedirectToAction(nameof(Index)));
            }
        }