Пример #1
0
        public static ChangeList ReadChanges(string url)
        {
            try
            {
                using (WebClient wc = new WebClient())
                {
                    string jsonString = wc.DownloadString(url);

                    ChangeList changeList = JsonSerializer.Deserialize <ChangeList>(jsonString);

                    return(changeList);
                };
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #2
0
        public static Mixtape ReadAndApplyChanges(Mixtape currentMixtape, string changes)
        {
            Mixtape    updatedMixtape = currentMixtape;
            ChangeList changeList     = ChangeList.ReadChanges(changes);

            foreach (Change c in changeList.changes)
            {
                try
                {
                    if (c.user is not null)
                    {
                        switch (c.change_action)
                        {
                        case ("update"):
                            updatedMixtape = UpdateUser(updatedMixtape, c.user);
                            break;

                        case ("add"):
                            updatedMixtape = AddUser(updatedMixtape, c.user);
                            break;

                        case ("delete"):
                            updatedMixtape = RemoveUser(updatedMixtape, c.user);
                            break;

                        default:
                            throw new NotSupportedException($"Unable to recognize {nameof(c.change_action)}: {c.change_action}\r\n"
                                                            + $"Could not make changes to user id: {c.user.id}");
                        }
                    }

                    if (c.playlist is not null)
                    {
                        switch (c.change_action)
                        {
                        case ("update"):
                            updatedMixtape = UpdatePlaylist(updatedMixtape, c.playlist);
                            break;

                        case ("add"):
                            updatedMixtape = AddPlaylist(updatedMixtape, c.playlist);
                            break;

                        case ("delete"):
                            updatedMixtape = RemovePlaylist(updatedMixtape, c.playlist);
                            break;

                        default:
                            throw new NotSupportedException($"Unable to recognize {nameof(c.change_action)}: {c.change_action}\r\n"
                                                            + $"Could not make changes to playlist id: {c.playlist.id}");
                        }
                    }

                    if (c.song is not null)
                    {
                        switch (c.change_action)
                        {
                        case ("update"):
                            updatedMixtape = UpdateSong(updatedMixtape, c.song);
                            break;

                        case ("add"):
                            updatedMixtape = AddSong(updatedMixtape, c.song);
                            break;

                        case ("delete"):
                            updatedMixtape = RemoveSong(updatedMixtape, c.song);
                            break;

                        default:
                            throw new NotSupportedException($"Unable to recognize {nameof(c.change_action)}: {c.change_action}\r\n"
                                                            + $"Could not make changes to song id: {c.song.id}");
                        }
                    }
                }
                catch (NotSupportedException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            return(updatedMixtape);
        }