Пример #1
0
        public PersonContent GetPersonContent(long personId)
        {
            using (var personRepository = new BaseRepository<Person>())
            {
                Person person = personRepository.GetAllItems.FirstOrDefault(x => x.Id == personId);
                if (person == null)
                {
                    throw new PersonDoesNotExistException();
                }
                // Fill person conten
                PersonContent content = new PersonContent();
                content.Id = person.Id;
                content.Gender = person.Gender;
                content.Email = person.Email;
                content.BirthDate = person.BirthDate.Value;
                content.FirstName = person.FirstName;
                content.LastName = person.LastName;
                content.MiddleName = person.MiddleName;
                content.Note = person.Note;
                if (person.Curator != null)
                {
                    content.CuratorId = person.Curator.Id;
                }
                else
                {
                    content.CuratorId = 0;
                }

                return content;
            }
        }
Пример #2
0
        public static PersonContent GetPerson(Guid token)
        {
            using (var personRepository = new BaseRepository<Person>())
            {
                Authorization authorization =
                    personRepository.Context.Authorizations.FirstOrDefault(x => x.Token == token);
                if (authorization == null)
                {
                    throw new UnauthorizedException();
                }
                Person person;
                if (authorization.SurveysPersonId != 0)
                {
                    SurveysPersonsGroup surveysPersonsGroup =
                        personRepository.Context.SurveysPersonsGroups.FirstOrDefault(
                            x => x.Id == authorization.SurveysPersonId);
                    if (surveysPersonsGroup == null)
                    {
                        throw new UnauthorizedException();
                    }
                    person = surveysPersonsGroup.Person;
                }
                else
                {
                    Client client = personRepository.Context.Clients.FirstOrDefault(x => x.Id == authorization.ClientId);
                    if (client == null)
                    {
                        throw new UnauthorizedException();
                    }
                    person = personRepository.GetAllItems.FirstOrDefault(x => x.Id == client.PersonId);
                }

                if (person == null)
                {
                    throw new UnauthorizedException();
                }

                // Form output person content
                PersonContent content = new PersonContent();

                content.Id = person.Id;
                content.BirthDate = person.BirthDate.Value;
                content.Gender = person.Gender;
                content.FirstName = person.FirstName;
                content.LastName = person.LastName;
                content.MiddleName = person.MiddleName;
                content.Email = person.Email;
                content.Note = person.Note;
                content.Token = authorization.Token;
                content.ExpirationDate = authorization.ExpirationDate;

                return content;

            }
        }
Пример #3
0
        public PersonContent AuthorizePerson(String accessKey)
        {
            using(var personRepository = new BaseRepository<Person>())
            {
                SurveysPersonsGroup surveysPersonsGroup =
                    personRepository.Context.SurveysPersonsGroups.FirstOrDefault(
                        x => String.Equals(x.AccessKey, accessKey));
                if(surveysPersonsGroup == null)
                {
                    throw new PersonDoesNotExistException();
                }
                Person person = surveysPersonsGroup.Person;

                Authorization authorization =
                    personRepository.Context.Authorizations.FirstOrDefault(
                        x => x.SurveysPersonId == surveysPersonsGroup.Id);
                if(authorization == null)
                {
                    authorization = new Authorization()
                                        {
                                            SurveysPersonId = surveysPersonsGroup.Id,
                                            Token = Guid.NewGuid(),
                                            ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes),
                                        };
                    personRepository.Context.Authorizations.Add(authorization);
                }
                else
                {
                    // Update row in Authorizations
                    if (authorization.ExpirationDate < DateTime.Now)
                    {
                        authorization.Token = Guid.NewGuid();
                    }
                    authorization.ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes);
                    personRepository.Context.Entry(authorization).State = EntityState.Modified;
                }
                personRepository.Context.SaveChanges();

                //Fill person content
                PersonContent content = new PersonContent();
                content.Id = surveysPersonsGroup.Id;
                content.PersonId = person.Id;
                content.AccessKey = surveysPersonsGroup.AccessKey;
                content.SurveyResultId = surveysPersonsGroup.SurveysResult.Id;
                content.Gender = person.Gender;
                content.Email = person.Email;
                content.BirthDate = person.BirthDate.Value;
                content.FirstName = person.FirstName;
                content.LastName = person.LastName;
                content.MiddleName = person.MiddleName;
                content.Note = person.Note;
                if (person.Curator != null)
                {
                    content.CuratorId = person.Curator.Id;
                }
                else
                {
                    content.CuratorId = 0;
                }
                content.ExpirationDate = authorization.ExpirationDate;
                content.Token = authorization.Token;
                //content.SurveysPersonId = surveysPersonsGroup.Id;

                return content;
            }
        }
Пример #4
0
        public void AddShellResult(PersonContent person, SurveysShellResult surveysShellResult, String testCodeName, String answerStr)
        {
            try
            {
                _connection.Open();
                int count = -1;

                // Check OldUser exists
                String name = person.FirstName + " " + person.LastName;
                if (name.Length == 1)
                {
                    name = String.Empty;
                }
                String gender = String.Empty;
                if (person.Gender)
                {
                    gender = "M";
                }
                else
                {
                    gender = "F";
                }
                String query = @"SELECT count(*) FROM [USER] where [NAME] = '" + name + "'" +
                           " AND [GENDER] = '" + gender + "' AND [DATE] = #" + person.BirthDate.ToString("yyyy-MM-dd") + "#";

                var command = new OleDbCommand(query, _connection);
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    count = reader.GetInt32(0);
                }
                if (count <= 0)
                {
                    // Cut name to 30 symbols
                    if (name.Length >= 30)
                    {
                        name = name.Remove(29);
                    }

                    // Create OldUser
                    query = "INSERT INTO [USER] (NAME, GENDER, [DATE]) VALUES('"
                        + name + "', '" + gender + "', #" + person.BirthDate.ToString("yyyy-MM-dd") + "#)";
                    command = new OleDbCommand(query, _connection);
                    if (command.ExecuteNonQuery() <= 0)
                    {
                        throw new ConsulDBException();
                    }

                }

                // GET id of OldUser
                int userId = -1;

                query = @"SELECT ID FROM [USER] where [NAME] = '" + name + "'" +
                        " AND [GENDER] = '" + gender + "' AND [DATE] = #" + person.BirthDate.ToString("yyyy-MM-dd") + "#";

                command = new OleDbCommand(query, _connection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    userId = reader.GetInt32(0);
                }

                var comment = surveysShellResult.TestDate.ToString("dd.MM.yyyy_HH.mm.ss") + "_"
                                  + person.FirstName + "_"
                                  + person.LastName + "_"
                                  + person.BirthDate.ToString("dd.MM.yyyy") + "_"
                                  + testCodeName;

                // Create result
                query = "INSERT INTO [RESULT] (U_ID, M_ID, DATA, COMMENT, [DATE]) VALUES ("
                        + userId + ", " + surveysShellResult.ShellMethodId + ", '" + answerStr + "', '" + comment + "', #" + surveysShellResult.TestDate.ToString("yyyy-MM-dd HH:mm:ss") + "#)";
                command = new OleDbCommand(query, _connection);
                if (command.ExecuteNonQuery() <= 0)
                {
                    throw new ConsulDBException();
                }

                _connection.Close();
            }
            catch (Exception)
            {
                throw new ConsulDBException();
            }
        }