Пример #1
0
        public static void ExportToFile(List <AlbumPlayed> albums, out string fileContent)
        {
            fileContent = string.Empty;

            AlbumPlayed currentAlbumPlayed = new AlbumPlayed {
                Date = DateTime.MinValue
            };

            foreach (var album in albums)
            {
                if (currentAlbumPlayed.Date.DayOfYear != album.Date.DayOfYear ||
                    currentAlbumPlayed.Date.Year != album.Date.Year)
                {
                    WriteNewDate(album.Date, ref fileContent);
                    currentAlbumPlayed.Date     = album.Date;
                    currentAlbumPlayed.Location = string.Empty;
                    currentAlbumPlayed.Artist   = string.Empty;
                }

                if (currentAlbumPlayed.Location != album.Location)
                {
                    WriteNewLocation(album.Location, ref fileContent);
                    currentAlbumPlayed.Location = album.Location;
                    currentAlbumPlayed.Artist   = string.Empty;
                }

                if (currentAlbumPlayed.Artist != album.Artist)
                {
                    WriteNewArtist(album.Artist, ref fileContent);
                    currentAlbumPlayed.Artist = album.Artist;
                }

                WriteNewAlbum(album.Album, ref fileContent);
            }
        }
Пример #2
0
        public IActionResult Post([FromBody] DisplayAlbum addAlbumRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            AlbumPlayedAddResponse response = new AlbumPlayedAddResponse
            {
                ErrorCode  = (int)UserResponseCode.Success,
                Album      = new DisplayAlbum(addAlbumRequest),
                FailReason = "",
                UserId     = ""
            };

            lock (Lock)
            {
                var newAlbum = new AlbumPlayed()
                {
                    Album      = addAlbumRequest.Album,
                    Artist     = addAlbumRequest.Artist,
                    Date       = addAlbumRequest.Date,
                    ImagePath  = addAlbumRequest.ImagePath,
                    Location   = addAlbumRequest.Location,
                    PlayerLink = addAlbumRequest.PlayerLink,
                    UserName   = addAlbumRequest.UserName
                };
                _albumPlayedDatabase.AddNewItemToDatabase(newAlbum);
            }

            return(Ok(response));
        }
Пример #3
0
        private static void ProcessLine(
            string line,
            ref FileReadState state,
            ref List <AlbumPlayed> albums,
            ref AlbumPlayed currentAlbumPlayed)
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                return;
            }

            switch (state)
            {
            case FileReadState.Reset:
                ParseLineWhenReset(line, ref state, ref currentAlbumPlayed);
                break;

            case FileReadState.OnDate:
                ParseLineWhenOnDate(line, ref state, ref currentAlbumPlayed);
                break;

            case FileReadState.OnLocation:
                ParseLineWhenOnLocation(line, ref state, ref currentAlbumPlayed);
                break;

            case FileReadState.OnArtist:
                ParseLineWhenOnArtist(line, ref state, ref currentAlbumPlayed, albums);
                break;
            }
        }
Пример #4
0
 private static void ParseLineWhenOnArtist(
     string line,
     ref FileReadState state,
     ref AlbumPlayed currentAlbumPlayed,
     List <AlbumPlayed> albums)
 {
     if (line[0] == '\t' && line[1] == '\t' && line[2] == '\t')
     {
         string album = line.TrimEnd(IgnoreChars).TrimStart(IgnoreChars);
         currentAlbumPlayed.Album = album;
         albums.Add(currentAlbumPlayed);
         currentAlbumPlayed      = new AlbumPlayed(currentAlbumPlayed);
         currentAlbumPlayed.Date = currentAlbumPlayed.Date.AddMinutes(1);
     }
     else if (line[0] == '\t' && line[1] == '\t')
     {
         ParseLineWhenOnLocation(line, ref state, ref currentAlbumPlayed);
     }
     else if (line[0] == '\t')
     {
         ParseLineWhenOnDate(line, ref state, ref currentAlbumPlayed);
     }
     else
     {
         ParseLineWhenReset(line, ref state, ref currentAlbumPlayed);
     }
 }
Пример #5
0
        public static List <AlbumPlayed> GetAlbumsPlayedFromFile(string filePath)
        {
            List <AlbumPlayed> albums = new List <AlbumPlayed>();

            // check file exists
            if (!File.Exists(filePath))
            {
                return(albums);
            }

            // default the parsing state data
            string        line;
            FileReadState parseState         = FileReadState.Reset;
            AlbumPlayed   currentAlbumPlayed = new AlbumPlayed();

            // Read the file and parse it line by line.
            StreamReader file = new StreamReader(filePath);

            while ((line = file.ReadLine()) != null)
            {
                ProcessLine(line, ref parseState, ref albums, ref currentAlbumPlayed);
            }

            file.Close();

            return(albums);
        }
Пример #6
0
 public DisplayAlbum(AlbumPlayed displayAlbum)
 {
     Id         = displayAlbum.Id.ToString();
     Date       = displayAlbum.Date;
     Location   = displayAlbum.Location;
     Artist     = displayAlbum.Artist;
     Album      = displayAlbum.Album;
     UserName   = displayAlbum.UserName;
     ImagePath  = displayAlbum.ImagePath;
     PlayerLink = displayAlbum.PlayerLink;
 }
Пример #7
0
 private static void ParseLineWhenOnLocation(
     string line,
     ref FileReadState state,
     ref AlbumPlayed currentAlbumPlayed)
 {
     if (line[0] == '\t' && line[1] == '\t')
     {
         string artist = line.TrimEnd(IgnoreChars).TrimStart(IgnoreChars);
         currentAlbumPlayed.Artist = artist;
         state = FileReadState.OnArtist;
     }
 }
Пример #8
0
 private static void ParseLineWhenOnDate(
     string line,
     ref FileReadState state,
     ref AlbumPlayed currentAlbumPlayed)
 {
     if (line[0] == '\t')
     {
         string location = line.TrimEnd(IgnoreChars).TrimStart(IgnoreChars);
         currentAlbumPlayed.Location = location;
         state = FileReadState.OnLocation;
     }
 }
Пример #9
0
        public SongsFilesDetailsResponse GetAllAlbumsPlayedFromFileUpdatesToUser(string fileKey, string userId)
        {
            var resp = GetAllAlbumsPlayedFromFile(fileKey);

            if (resp.ErrorCode == (int)SongsFilesResponseCode.Success)
            {
                User foundUser = _userDatabase.LoadedItems.FirstOrDefault(x => x.Id.ToString() == userId);

                if (foundUser == null)
                {
                    resp.ErrorCode  = (int)SongsFilesResponseCode.InvalidUser;
                    resp.FailReason = "No user for this id.";
                }
                else
                {
                    // Add the more recent albums read from file with this user name
                    lock (Lock)
                    {
                        // Get the existing items for this user from the table
                        AlbumPlayed latestAlbum =
                            _albumPlayedDatabase.LoadedItems
                            .Where(x => x.UserName == foundUser.Name)
                            .OrderBy(x => x.Date).Last();

                        if (latestAlbum != null)
                        {
                            foreach (var fileAlbum in resp.AlbumsPlayed.OrderBy(x => x.Date))
                            {
                                if (latestAlbum.Date < fileAlbum.Date)
                                {
                                    _albumPlayedDatabase.AddNewItemToDatabase(
                                        new AlbumPlayed
                                    {
                                        Date       = fileAlbum.Date,
                                        Location   = fileAlbum.Location,
                                        Artist     = fileAlbum.Artist,
                                        Album      = fileAlbum.Album,
                                        ImagePath  = fileAlbum.ImagePath,
                                        PlayerLink = fileAlbum.PlayerLink,

                                        UserName = foundUser.Name
                                    }
                                        );
                                }
                            }
                        }
                    }
                }
            }

            return(resp);
        }
Пример #10
0
        private static void GetTestAlbumPlayed(out string albumPlayedName, out AlbumPlayed albumPlayed)
        {
            albumPlayedName = Guid.NewGuid().ToString();
            string artist   = "a test artist";
            string location = "To Work";
            string userName = "******";

            albumPlayed = new AlbumPlayed
            {
                Album    = albumPlayedName,
                Artist   = artist,
                Date     = DateTime.Now,
                Location = location,
                UserName = userName
            };
        }
Пример #11
0
        public static void CheckAlbum_0(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(23, album.Date.Day);

            // Check the location, artist & album etc
            Assert.AreEqual("To Work", album.Location);
            Assert.AreEqual("Current 93", album.Artist);
            Assert.AreEqual("Halo", album.Album);

            Assert.AreEqual("https://upload.wikimedia.org/wikipedia/en/f/ff/Halo_album_cover.jpg", album.ImagePath);
            Assert.AreEqual("https://www.youtube.com/watch?v=i_sBbgptcQE", album.PlayerLink);
            Assert.AreEqual("JMcR", album.UserName);
        }
Пример #12
0
        public static void CheckAlbum_8(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(25, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("The Fall", album.Artist);
            Assert.AreEqual("Sub-Lingual Tablet", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #13
0
        public static void CheckAlbum_4(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(24, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("Nick Cave & the Bad Seeds", album.Artist);
            Assert.AreEqual("Live from KCRW", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #14
0
 private static void ParseLineWhenReset(
     string line,
     ref FileReadState state,
     ref AlbumPlayed currentAlbumPlayed)
 {
     if (char.IsLetterOrDigit(line[0]))
     {
         var words = line.Split(' ');
         if (words.Length >= NumberWordsInDate)
         {
             DateTime date = DateTime.Now;
             if (ParseDateFromString(words, ref date))
             {
                 currentAlbumPlayed.Date = date;
                 state = FileReadState.OnDate;
             }
         }
     }
 }
Пример #15
0
        public static void CheckAlbum_5(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(24, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("Acid Mothers Temple & The Melting Paraiso U.F.O.", album.Artist);
            Assert.AreEqual("IAO Chant From the Melting Paraiso Underground Freak Out", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #16
0
        public static void CheckAlbum_Last(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2019, album.Date.Year);
            Assert.AreEqual(8, album.Date.Month);
            Assert.AreEqual(7, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("Trash Kit", album.Artist);
            Assert.AreEqual("Confidence", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #17
0
        public static void CheckAlbum_3(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(24, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("To Work", album.Location);
            Assert.AreEqual("Current 93", album.Artist);
            Assert.AreEqual("Halo", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #18
0
        public static void CheckAlbum_2(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(23, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("Lubomyr Melnyk", album.Artist);
            Assert.AreEqual("Rivers And Streams", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }
Пример #19
0
        public static void CheckAlbum_1(AlbumPlayed album)
        {
            Assert.IsNotNull(album);

            // Check the date
            Assert.AreEqual(2018, album.Date.Year);
            Assert.AreEqual(1, album.Date.Month);
            Assert.AreEqual(23, album.Date.Day);

            // Check the location, artist & album
            Assert.AreEqual("In Work", album.Location);
            Assert.AreEqual("Alice Coltrane", album.Artist);
            Assert.AreEqual("World Spirituality Classics 1: The Ecstatic Music of Alice Coltrane Turiyasangitananda", album.Album);

            // check the other items are still blank
            Assert.AreEqual(string.Empty, album.ImagePath);
            Assert.AreEqual(string.Empty, album.PlayerLink);
            Assert.AreEqual(string.Empty, album.UserName);
        }