public byte UpdateExistedAccount(int id, AccountUpdatDto dto) { if (!_inMemory.IsExistedAccountId(id)) { return(1); //throw new AccountNotFoundException($"Account with id {id} not exists"); } if (dto.Email != null && (!IsValidEmail(dto.Email) || _inMemory.Emails.Contains(dto.Email))) { return(2); //throw new InvalidUpdateException($"Email {dto.Email} already existed"); } if (dto.Sex != null && !_inMemory.SexData.ContainsValue(dto.Sex)) { return(2); //throw new InvalidUpdateException($"Sex {dto.Sex} invalid"); } if (dto.Status != null && !_inMemory.StatusData.ContainsValue(dto.Status)) { return(2); //throw new InvalidUpdateException($"Sex {dto.Status} invalid"); } //lock (_inMemory) //{ UpdateExistedAccountImpl(id, dto); //} return(0); }
private void UpdateExistedAccountImpl(int id, AccountUpdatDto dto) { var acc = _inMemory.Accounts[id]; var newPremium = acc.PremiumIndex; var newStatus = acc.Status; var newSex = acc.Sex; var prevPremium = acc.PremiumIndex; var prevStatus = acc.Status; var prevSex = acc.Sex; // FName if (dto.FName != null) { acc.FNameIndex = _inMemory.FNameData.UpdateOrAdd(dto.FName, id, acc.FNameIndex); } // SName if (dto.SName != null) { acc.SNameIndex = _inMemory.SNameData.UpdateOrAdd(dto.SName, id, acc.SNameIndex); } // Phone and Code if (!string.IsNullOrEmpty(dto.Phone)) { var code = GetPhoneCode(dto.Phone); acc.Phone = dto.Phone; acc.CodeIndex = _inMemory.CodeData.UpdateOrAdd(code, id, acc.CodeIndex); } // Domain and Email if (dto.Email != null) { _inMemory.Emails.Remove(dto.Email); acc.Email = dto.Email; _inMemory.Emails.Add(dto.Email); int atIndex = dto.Email.IndexOf('@'); var domain = dto.Email.Substring(atIndex + 1); acc.DomainIndex = _inMemory.DomainData.UpdateOrAdd(domain, id, acc.DomainIndex); } // Sex if (dto.Sex != null) { var sexIndex = _inMemory.SexData.GetIndex(dto.Sex); acc.Sex = sexIndex; _inMemory.SexData.Update(sexIndex, id, acc.Sex); newSex = acc.Sex; } // Birth if (dto.Birth.HasValue) { acc.Birth = dto.Birth.Value; var birthYear = GetYearFromTs(dto.Birth.Value); acc.BirthYearIndex = _inMemory.BirthYearData.UpdateOrAdd(birthYear, id, acc.BirthYearIndex); } // Country if (dto.Country != null) { acc.CountryIndex = _inMemory.CountryData.UpdateOrAdd(dto.Country, id, acc.CountryIndex); } // City if (dto.City != null) { acc.CityIndex = _inMemory.CityData.UpdateOrAdd(dto.City, id, acc.CityIndex); } // Joined if (dto.Joined.HasValue) { var joinedYear = GetYearFromTs(dto.Joined.Value); acc.JoinedYearIndex = _inMemory.JoinedYearData.UpdateOrAdd(joinedYear, id, acc.JoinedYearIndex); } // Status if (dto.Status != null) { var statusIndex = _inMemory.StatusData.GetIndex(dto.Status); _inMemory.StatusData.Update(statusIndex, id, acc.Status); acc.Status = statusIndex; newStatus = statusIndex; } // Premium if (dto.Premium != null) { var previouslyExpired = acc.PremiumStart > 0; acc.PremiumStart = dto.Premium.Start; acc.PremiumFinish = dto.Premium.Finish; byte premium = 0; if (dto.Premium.Start < NowTs && dto.Premium.Finish > NowTs) { premium = 1; } _inMemory.PremiumData.Update(premium, id, acc.PremiumIndex, previouslyExpired, acc.PremiumStart > 0); acc.PremiumIndex = premium; newPremium = premium; } // Interests if (dto.Interests != null) { acc.Interests = _inMemory.InterestsData.UpdateOrAdd(dto.Interests, id, acc.Interests ?? Enumerable.Empty <byte>(), prevPremium, prevStatus, prevSex, newPremium, newStatus, newSex) .ToArray(); Array.Sort(acc.Interests); } else if ((prevSex != newSex || prevStatus != newStatus || prevPremium != newPremium) && acc.Interests != null) { _inMemory.InterestsData.UpdateRecommendationsData(id, acc.Interests, prevPremium, prevStatus, prevSex, newPremium, newStatus, newSex); } // Likes if (dto.Likes != null) { acc.AddLikesFromToNewAccount(dto.Likes.Select(x => x.Id).ToArray()); foreach (var like in dto.Likes) { _inMemory.Accounts[like.Id].AddLikeTo(id, like.TimeStamp); } } }