private int IsExistClient(ClientDTO clientDTO) { var client = repositories.ClientRepos.Get().Where(c => c.Account.Email == clientDTO.Account.Email || c.Account.Phone == clientDTO.Account.Phone).FirstOrDefault(); if (client == null) { return(-1); } else { return(client.Id); } }
public ClientDTO CreateNewClient(ClientDTO client, string password) { var id = IsExistClient(client); if (id == -1 && !IsExistUniqueName(client.Id, client.UniqueName, true)) { client.Account.Password = password; repositories.ClientRepos.Insert(clientMapper.Map <Client>(client)); repositories.Save(); repositories.ClientRepos.Get().FirstOrDefault(c => c.UniqueName == client.UniqueName).AccountId = repositories.AccountRepos.Get().FirstOrDefault(a => a.Email == client.Account.Email).ClientId; repositories.Save(); ClientDTO addedClient = clientMapper.Map <ClientDTO>(repositories.ClientRepos.Get().FirstOrDefault(c => c.UniqueName == client.UniqueName)); callbacks.Add(new CallBack() { ClientId = addedClient.Id, Callback = OperationContext.Current.GetCallbackChannel <ICallback>() }); return(addedClient); } else { return(null); } }
public void SetProperties(ClientDTO client, out bool result) { var id = IsNotExistClient(client); if (id == -1) { var oldClient = repositories.ClientRepos.Get().Where(c => c.Id == client.Id).FirstOrDefault(); var pmChats = repositories.ChatRepos.Get().Where(c => c.IsPM && c.ChatMembers.Where(cm => cm.Client.Id == oldClient.Id).Count() > 0); foreach (var item in pmChats) { item.Name = item.Name.Replace(oldClient.Name, client.Name); foreach (var item2 in callbacks) { if (item2.ClientId != client.Id) { var oponent = item.ChatMembers.FirstOrDefault(cm => cm.Client.Id != oldClient.Id); if (oponent != null) { if (oponent.Client.Id == item2.ClientId) { item2.Callback.SetNewPMChatProperties(chatMapper.Map <ChatDTO>(item)); } } } } } repositories.Save(); oldClient.Name = client.Name; oldClient.UniqueName = client.UniqueName; oldClient.Account.Email = client.Account.Email; oldClient.Account.Phone = client.Account.Phone; repositories.ClientRepos.Update(oldClient); repositories.Save(); result = true; foreach (var item in callbacks) { if (item.ClientId != client.Id) { var contacts = TakeContacts(item.ClientId); if (contacts.FirstOrDefault(c => c.Id == client.Id) != null) { try { item.Callback.GetNewClientProperties(clientMapper.Map <ClientDTO>(oldClient)); } catch (Exception) { } } } } foreach (var item in callbacks) { if (item.ClientId != client.Id) { var chats = TakeChats(item.ClientId); foreach (var item2 in chats) { var members = TakeClients(item2.Id); foreach (var item3 in members) { if (item3.ClientId == client.Id) { try { item.Callback.GetNewClientProperties(clientMapper.Map <ClientDTO>(oldClient)); } catch (Exception) { } } } } } } } else { result = false; } }