Example #1
0
        public async Task <UserLogin> SignUp(string Identifier, string Credentials, int Type)
        {
            var ExistsUser = _context.UserLogin.FirstOrDefault(x => x.Identifier == Identifier && x.Type == Type);

            if (ExistsUser != null)
            {
                return(new UserLogin());
            }

            var transaction = await _context.Database.BeginTransactionAsync();

            try
            {
                var userid = _uid.NextId().ToString();
                // Add a new user login info.
                var NewUser = new UserLogin {
                    Credentials = Credentials, Identifier = Identifier, Type = (byte)Type, Userid = userid
                };
                _context.UserLogin.Add(NewUser);

                // Add a new blank user profile.
                var NewUserProfile = new UserProfile {
                    Userid = userid, Nickname = $"新用户{userid.Substring(8)}"
                };
                _context.UserProfile.Add(NewUserProfile);

                // Add a new user profile privacy configuration. default all profile fields are public.
                var NewUserProfilePrivacyConfig = new UserProfilePrivacy {
                    Userid = userid
                };
                _context.UserProfilePrivacy.Add(NewUserProfilePrivacyConfig);

                int affects = await _context.SaveChangesAsync();

                if (affects == 3)
                {
                    _singer.CreateUserSavedSingerList(userid);
                    transaction.Commit();
                    return(NewUser);
                }
                _logger.LogError($"The number of affected records is wrong: {affects}. Rollback.");
                transaction.Rollback();
                return(null);
            }
            catch (Exception e)
            {
                _logger.LogError($"Database update mistake! {e.Message}");
                transaction.Rollback();
            }
            return(null);
        }
Example #2
0
        public async Task <CommonResponse> UpdateProfilePrivacy(string QueryUserID, UserProfilePrivacy updatedPrivacy)
        {
            var oldPrivacy = GetProfilePrivacy(QueryUserID);

            if (oldPrivacy != null)
            {
                var merged = TypeMerger.MergeProperties(oldPrivacy, updatedPrivacy, "Userid");
                if (await _users.UpdateProfilePrivacy(merged))
                {
                    return new CommonResponse {
                               StatusCode = 0
                    }
                }
                ;
            }

            return(new CommonResponse {
                StatusCode = -1
            });
        }
Example #3
0
        private UserProfile ApplyPrivacyToUserProfile(UserProfile profile, UserProfilePrivacy privacy, bool IsSelf = false)
        {
            if (IsSelf)
            {
                return(profile);
            }
            foreach (var item in ProfileProperties)
            {
                var gate = ProfileGates.FirstOrDefault(x => x.Name == item.Name);
                if (gate != null)
                {
                    var gateBit = (byte)gate.GetValue(privacy);

                    if (gateBit == 0)
                    {
                        item.SetValue(profile, GetDefaultValue(item.PropertyType));
                    }
                }
            }
            return(profile);
        }
Example #4
0
        public async Task <bool> UpdateProfilePrivacy(UserProfilePrivacy privacy)
        {
            var tr = await _context.Database.BeginTransactionAsync();

            try
            {
                _context.UserProfilePrivacy.Update(privacy);
                if (1 == await _context.SaveChangesAsync())
                {
                    tr.Commit();
                    return(true);
                }
                tr.Rollback();
                return(false);
            }
            catch (Exception e)
            {
                _logger.LogError($"Database update mistake! {e.Message}");
                tr.Rollback();
            }
            return(false);
        }
Example #5
0
        public async Task <JsonResult> UpdateProfilePrivacy([FromBody] UserProfilePrivacy updatedPrivacy)
        {
            string UserID = User.Claims.ToList()[0].Value;

            return(new JsonResult(await _profile.UpdateProfilePrivacy(UserID, updatedPrivacy)));
        }