Exemple #1
0
        public async Task CreateChat(Guid userGuid, Guid creatorGuid)
        {
            using (var context = new MessengerContext())
            {
                Guid    dbChatGuid = Guid.NewGuid();
                Db.Chat dbChat     = new Db.Chat
                {
                    Guid = dbChatGuid
                };

                context.Chat.Add(dbChat);

                Db.Party party1 = new Db.Party
                {
                    ChatGuid = dbChatGuid,
                    UserGuid = creatorGuid,
                };

                Db.Party party2 = new Db.Party
                {
                    ChatGuid = dbChatGuid,
                    UserGuid = userGuid,
                };

                context.Party.Add(party1);
                context.Party.Add(party2);

                await context.SaveChangesAsync();
            }
        }
Exemple #2
0
        public async Task <string> AddEmployee(Employee employee)
        {
            var password = Helper.CreateRandomPassword(8);

            using (var context = new MessengerContext())
            {
                var dbEmployee = new Db.Employee
                {
                    Surname     = employee.Surname,
                    Name        = employee.Name,
                    Patronymic  = employee.Patronymic,
                    Gender      = employee.Gender,
                    DateOfBirth = employee.DateOfBirth,
                    Position    = employee.Position,
                };

                context.Employee.Add(dbEmployee);

                var dbUser = new Db.User()
                {
                    Email    = employee.Email,
                    Login    = employee.Login,
                    Password = password.HashPassword()
                };

                context.User.Add(dbUser);
                await context.SaveChangesAsync();

                return(password);
            }
        }
Exemple #3
0
        public async Task CreateGroupChat(string chatName, Guid creatorGuid, List <Guid> userGuids)
        {
            using (var context = new MessengerContext())
            {
                var dbChatGuid = Guid.NewGuid();
                var dbChat     = new Db.Chat();
                dbChat.UserGuid = creatorGuid;
                dbChat.Name     = chatName;
                dbChat.Guid     = dbChatGuid;
                context.Chat.Add(dbChat);

                foreach (var userGuid in userGuids)
                {
                    var dbParty = new Db.Party();
                    dbParty.UserGuid = userGuid;
                    dbParty.ChatGuid = dbChatGuid;
                    context.Party.Add(dbParty);
                }

                var dbSelfParty = new Db.Party();
                dbSelfParty.UserGuid = creatorGuid;
                dbSelfParty.ChatGuid = dbChatGuid;
                context.Party.Add(dbSelfParty);

                await context.SaveChangesAsync();
            }
        }
Exemple #4
0
 public async Task UpdatePhoto(Guid userGuid, string photo)
 {
     using (var context = new MessengerContext())
     {
         var user = context.Employee.Single(x => x.Guid == userGuid);
         user.Photo = photo;
         await context.SaveChangesAsync();
     }
 }
Exemple #5
0
        public async Task <ResultBody> ChangePassword(Guid selfGuid, string oldPassword, string newPassword)
        {
            using (var context = new MessengerContext())
            {
                if (String.IsNullOrEmpty(newPassword))
                {
                    return(new ResultBody {
                        ResultStatus = ResultStatus.NotSuccess, Message = "Пароль пуст!"
                    });
                }

                var user = await context.User.SingleOrDefaultAsync(x => x.Guid == selfGuid);

                if (user == null)
                {
                    return(new ResultBody {
                        ResultStatus = ResultStatus.NotSuccess, Message = "Пользователя с таким паролем не существует!"
                    });
                }

                if (user.Password != oldPassword.HashPassword())
                {
                    return(new ResultBody {
                        ResultStatus = ResultStatus.NotSuccess, Message = "Старый пароль не совпадает!"
                    });
                }

                var password = newPassword.HashPassword();
                if (password == newPassword)
                {
                    return(new ResultBody {
                        ResultStatus = ResultStatus.NotSuccess, Message = "Новый пароль совпадает с новым!"
                    });
                }

                user.Password = password;

                await context.SaveChangesAsync();

                return(new ResultBody {
                    ResultStatus = ResultStatus.Success
                });
            }
        }
Exemple #6
0
        public async Task RemoveEmployee(Employee employee)
        {
            using (var context = new MessengerContext())
            {
                var dbEmployee = await context.Employee.SingleOrDefaultAsync(x =>
                                                                             x.Surname.Equals(employee.Surname) &&
                                                                             x.Name.Equals(employee.Name) &&
                                                                             x.Patronymic.Equals(employee.Patronymic));

                if (dbEmployee == null)
                {
                    return;
                }

                var userGuid = dbEmployee.Guid;
                var dbUser   = await context.User.SingleAsync(x => x.Guid == userGuid);

                context.Employee.Remove(dbEmployee);
                context.User.Remove(dbUser);
                await context.SaveChangesAsync();
            }
        }
Exemple #7
0
        public async Task SaveMessage(Guid selfGuid, Guid chatGuid, string message, DateTime date)
        {
            using (var context = new MessengerContext())
            {
                var messageGuid = Guid.NewGuid();
                var dbParties   = context.Party.Where(x => x.ChatGuid == chatGuid);
                if (dbParties.Any())
                {
                    var dbMessage = new Db.Message
                    {
                        Guid       = messageGuid,
                        UserGuid   = selfGuid,
                        Content    = message,
                        ChatGuid   = chatGuid,
                        DateCreate = date
                    };

                    context.Message.Add(dbMessage);

                    var partyUserGuids = dbParties.Select(x => x.UserGuid);
                    List <Db.MessageStatus> messageStatuses = new List <Db.MessageStatus>();
                    foreach (var partyUserGuid in partyUserGuids)
                    {
                        var messageStatus = new Db.MessageStatus
                        {
                            MessageGuid = messageGuid,
                            IsRead      = false,
                            UserGuid    = partyUserGuid
                        };

                        messageStatuses.Add(messageStatus);
                    }

                    context.MessageStatus.AddRange(messageStatuses);
                }

                await context.SaveChangesAsync();
            }
        }