private Response SaveHeadphones()
        {
            var settings = this.Bind <HeadphonesSettings>();

            var valid = this.Validate(settings);

            if (!valid.IsValid)
            {
                var error = valid.SendJsonError();
                Log.Info("Error validating Headphones settings, message: {0}", error.Message);
                return(Response.AsJson(error));
            }
            Log.Trace(settings.DumpJson());

            var result = HeadphonesService.SaveSettings(settings);

            Log.Info("Saved headphones settings, result: {0}", result);
            return(Response.AsJson(result
                ? new JsonResponseModel {
                Result = true, Message = "Successfully Updated the Settings for Headphones!"
            }
                : new JsonResponseModel {
                Result = false, Message = "Could not update the settings, take a look at the logs."
            }));
        }
        private Negotiator Headphones()
        {
            var settings = HeadphonesService.GetSettings();

            return(View["Headphones", settings]);
        }
        private Response RequestAlbum(string releaseId)
        {
            var settings        = PrService.GetSettings();
            var existingRequest = RequestService.CheckRequest(releaseId);

            Log.Debug("Checking for an existing request");

            if (existingRequest != null)
            {
                Log.Debug("We do have an existing album request");
                if (!existingRequest.UserHasRequested(Username))
                {
                    Log.Debug("Not in the requested list so adding them and updating the request. User: {0}", Username);
                    existingRequest.RequestedUsers.Add(Username);
                    RequestService.UpdateRequest(existingRequest);
                }
                return(Response.AsJson(new JsonResponseModel {
                    Result = true, Message = settings.UsersCanViewOnlyOwnRequests ? $"{existingRequest.Title} was successfully added!" : $"{existingRequest.Title} has already been requested!"
                }));
            }


            Log.Debug("This is a new request");

            var      albumInfo = MusicBrainzApi.GetAlbum(releaseId);
            DateTime release;

            DateTimeHelper.CustomParse(albumInfo.ReleaseEvents?.FirstOrDefault()?.date, out release);

            var artist = albumInfo.ArtistCredits?.FirstOrDefault()?.artist;

            if (artist == null)
            {
                return(Response.AsJson(new JsonResponseModel {
                    Result = false, Message = "We could not find the artist on MusicBrainz. Please try again later or contact your admin"
                }));
            }

            var albums        = Checker.GetPlexAlbums();
            var alreadyInPlex = Checker.IsAlbumAvailable(albums.ToArray(), albumInfo.title, release.ToString("yyyy"), artist.name);

            if (alreadyInPlex)
            {
                return(Response.AsJson(new JsonResponseModel
                {
                    Result = false,
                    Message = $"{albumInfo.title} is already in Plex!"
                }));
            }

            var img = GetMusicBrainzCoverArt(albumInfo.id);

            Log.Trace("Album Details:");
            Log.Trace(albumInfo.DumpJson());
            Log.Trace("CoverArt Details:");
            Log.Trace(img.DumpJson());


            var model = new RequestedModel
            {
                Title          = albumInfo.title,
                MusicBrainzId  = albumInfo.id,
                Overview       = albumInfo.disambiguation,
                PosterPath     = img,
                Type           = RequestType.Album,
                ProviderId     = 0,
                RequestedUsers = new List <string> {
                    Username
                },
                Status        = albumInfo.status,
                Issues        = IssueState.None,
                RequestedDate = DateTime.UtcNow,
                ReleaseDate   = release,
                ArtistName    = artist.name,
                ArtistId      = artist.id
            };

            if (ShouldAutoApprove(RequestType.Album, settings))
            {
                Log.Debug("We don't require approval OR the user is in the whitelist");
                var hpSettings = HeadphonesService.GetSettings();

                Log.Trace("Headphone Settings:");
                Log.Trace(hpSettings.DumpJson());

                if (!hpSettings.Enabled)
                {
                    RequestService.AddRequest(model);
                    return
                        (Response.AsJson(new JsonResponseModel
                    {
                        Result = true,
                        Message = $"{model.Title} was successfully added!"
                    }));
                }

                var sender = new HeadphonesSender(HeadphonesApi, hpSettings, RequestService);
                sender.AddAlbum(model).Wait();
                model.Approved = true;
                RequestService.AddRequest(model);

                if (ShouldSendNotification())
                {
                    var notify2 = new NotificationModel
                    {
                        Title            = model.Title,
                        User             = Username,
                        DateTime         = DateTime.Now,
                        NotificationType = NotificationType.NewRequest
                    };
                    NotificationService.Publish(notify2);
                }

                return
                    (Response.AsJson(new JsonResponseModel
                {
                    Result = true,
                    Message = $"{model.Title} was successfully added!"
                }));
            }

            if (ShouldSendNotification())
            {
                var notify2 = new NotificationModel
                {
                    Title            = model.Title,
                    User             = Username,
                    DateTime         = DateTime.Now,
                    NotificationType = NotificationType.NewRequest
                };
                NotificationService.Publish(notify2);
            }
            var result = RequestService.AddRequest(model);

            return(Response.AsJson(new JsonResponseModel
            {
                Result = true,
                Message = $"{model.Title} was successfully added!"
            }));
        }