示例#1
0
        public async Task <Models.User> GetOrCreateUserFromInfo(LastFmUserInfo userInfo)
        {
            var existingUser = await _context.Users
                               .Where(user => user.UserName == userInfo.Name)
                               .FirstOrDefaultAsync();

            if (existingUser != null)
            {
                return(existingUser);
            }

            var registeredAt = DateTimeOffset
                               .FromUnixTimeSeconds(userInfo.RegisterDate)
                               .DateTime;

            var newUser = new Models.User()
            {
                UserName       = userInfo.Name,
                RegisteredAt   = registeredAt,
                RealName       = userInfo.RealName,
                ProfilePicture = userInfo.Image,
                LastFmUrl      = userInfo.Url,
            };

            await _context.Users.AddAsync(newUser);

            await _context.SaveChangesAsync();

            return(newUser);
        }
        public async Task DeserializeUserInfo_WithValidJson_ReturnsCorrectUserInfo()
        {
            // Arrange
            var deserializer = MakeDeserializer();
            var jsonStream   = MakeJsonStreamFrom(LastFmJsonDeserializerData.UserInfoJson);

            // Act
            var actualUserInfo = await deserializer.DeserializeUserInfo(jsonStream);

            // Assert
            var expectedUserInfo = new LastFmUserInfo()
            {
                Name         = "edxds",
                RealName     = "",
                Url          = "https://www.last.fm/user/edxds",
                Image        = "https://lastfm.freetls.fastly.net/i/u/300x300/e4612070134f0e1b58968471969d54da.png",
                RegisterDate = 1501983583,
            };

            actualUserInfo.Should().BeEquivalentTo(expectedUserInfo);
        }