Exemplo n.º 1
0
        public ResponceModel <UserAuthModel> Autorize(string loginOrEmail, string password)
        {
            if (string.IsNullOrEmpty(loginOrEmail) || string.IsNullOrEmpty(password))
            {
                return(new ResponceModel <UserAuthModel>().FieldEmptyError());
            }

            var foundUser = _store.Users.Where(f => f.Login == loginOrEmail).Include(f => f.UserSessions).FirstOrDefault();

            if (foundUser == null)
            {
                foundUser = _store.Users.Where(f => f.Email == loginOrEmail).Include(f => f.UserSessions).FirstOrDefault();
            }

            if (foundUser == null)
            {
                return(new ResponceModel <UserAuthModel>().UserNotFound());
            }

            if (!foundUser.IsPasswordRight(password))
            {
                return(new ResponceModel <UserAuthModel>().WrongPassword());
            }

            foundUser.UserSessions.Where(f => f.IsActive == true).ToList().ForEach(f => f.IsActive = false);

            UserSession session = new UserSession()
            {
                UserID = foundUser.ID
            };

            _store.Add(session);
            _store.SaveChanges();

            return(new ResponceModel <UserAuthModel>()
            {
                content = new UserAuthModel
                {
                    User = foundUser,
                    UserSession = session
                }
            });
        }
Exemplo n.º 2
0
        public ResponceModel <Req> StoreOne <Req>(RequestModel <Req> request) where Req : IValidate
        {
            var responce = CheckRequest(request);

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

            _store.Add(request.Content);
            _store.SaveChanges();

            return(new ResponceModel <Req> {
                content = request.Content
            });
        }