Esempio n. 1
0
        public RegistrationContent Register(RegistrationContent registratedUser)
        {
            using (var clientRepository = new BaseRepository<Client>())
            {
                // Check required fields
                if (String.IsNullOrWhiteSpace(registratedUser.Login) || String.IsNullOrWhiteSpace(registratedUser.Email) ||
                    !registratedUser.BirthDate.HasValue || String.IsNullOrWhiteSpace(registratedUser.Password) ||
                    !registratedUser.Gender.HasValue)
                {
                    throw new RequireFieldException();
                }

                // Check password validation
                if(registratedUser.Password.Length < 6)
                {
                    throw new UserPasswordException();
                }

                // Check Login unique
                // SELECT Login from Clients
                if (clientRepository.GetAllItems.Any(x => String.Equals(x.Login, registratedUser.Login)))
                {
                    throw new UserLoginException();
                }

                // Check Email unique
                // SELECT Email from Persons WHERE Curator_Id == null
                if (clientRepository.Context.Persons.Include("Curator").Any(x => String.Equals(x.Email, registratedUser.Email) && x.Curator == null))
                {
                    throw new UserEmailException();
                }

                // Add row in Persons
                Person person = new Person()
                                    {
                                        FirstName = registratedUser.FirstName,
                                        LastName = registratedUser.LastName,
                                        MiddleName = registratedUser.MiddleName,
                                        BirthDate = registratedUser.BirthDate.Value,
                                        Gender = registratedUser.Gender.Value,
                                        Email = registratedUser.Email
                                    };
                clientRepository.Context.Persons.Add(person);
                clientRepository.Context.SaveChanges();

                // Add row in Clients
                Client client = new Client()
                                    {
                                        Login = registratedUser.Login,
                                        Password = registratedUser.Password,
                                        PersonId = person.Id,
                                        IsCurator = false,
                                        IsAdmin = false
                                    };
                if(!clientRepository.Create(client).Status)
                {
                    throw new CreateException();
                }

                // Add row in Authorizations
                Authorization authorization = new Authorization();
                authorization.Token = Guid.NewGuid();
                authorization.ClientId = client.Id;
                authorization.ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes);

                clientRepository.Context.Authorizations.Add(authorization);
                clientRepository.Context.SaveChanges();

                registratedUser.Id = client.Id;
                registratedUser.IsCurator = client.IsCurator;
                registratedUser.IsAdmin = client.IsAdmin;
                registratedUser.PersonId = person.Id;
                registratedUser.Token = authorization.Token;
                registratedUser.ExpirationDate = authorization.ExpirationDate;

                return registratedUser;

            }
        }
Esempio n. 2
0
        public Person CreateCuratorPerson(long curatorId, Person person)
        {
            using (var personRepository = new BaseRepository<Person>())
            {
                Client curator = personRepository.Context.Clients.FirstOrDefault(x => x.Id == curatorId);
                if (curator == null)
                {
                    throw new UserDoesNotExistException();
                }

                if (String.IsNullOrWhiteSpace(person.Email) || String.IsNullOrWhiteSpace(person.FirstName)
                    || String.IsNullOrWhiteSpace(person.LastName) || String.IsNullOrWhiteSpace(person.MiddleName)
                    || !person.BirthDate.HasValue)
                {
                    throw new RequireFieldException();
                }

                if (person.PersonsGroup != null)
                {
                    PersonsGroup personsGroup =
                    personRepository.Context.PersonsGroups.FirstOrDefault(x => x.Id == person.PersonsGroup.Id);
                    if (person.PersonsGroup.Id != 0 && personsGroup == null)
                    {
                        throw new PersonsGroupDoesNotExistException();
                    }
                    person.PersonsGroup = personsGroup;
                }

                // Check unique Email
                // select Email from Persons where Curator_Id = curatorId
                //if (personRepository.GetAllItems.Any(x => x.Curator.Id == curatorId && x.Email == person.Email))
                //{
                //    throw new PersonEmailException();
                //}

                person.Curator = curator;

                if (!personRepository.Create(person).Status)
                {
                    throw new CreateException();
                }

                return person;
            }
        }
Esempio n. 3
0
        public Object UpdateCuratorPerson(long curatorId, long personId, [FromBody] WebPersonInput webPerson)
        {
            WebJsonResponse response = new WebJsonResponse();
            try
            {
                Guid token = WebRequestHeaders.GetAuthorizationToken(this);

                //if (!ClientRepository.CheckAuthorization(token, curatorId, false))
                //{
                //    return new HttpResponseMessage(HttpStatusCode.Unauthorized);
                //}

                PersonContent personAuth = ClientRepository.GetPerson(token);
                if (!ClientRepository.CheckClientRights(personAuth.Id, curatorId))
                {
                    return new HttpResponseMessage(HttpStatusCode.MethodNotAllowed);
                }

                // Manual mapping
                Person person = new Person()
                {
                    Id = webPerson.Id,
                    BirthDate = webPerson.BirthDate,
                    Email = webPerson.Email,
                    FirstName = webPerson.FirstName,
                    LastName = webPerson.LastName,
                    MiddleName = webPerson.MiddleName,
                    Gender = webPerson.Gender,
                    Note = webPerson.Note
                };
                if (webPerson.PersonsGroupId.HasValue) // 0, 1, 2, ...
                {
                    person.PersonsGroup = new PersonsGroup() { Id = (Int64)webPerson.PersonsGroupId };
                }
                _curatorRepository.UpdateCuratorPerson(curatorId, personId,person);
                response.Data = new object();
                response.Status = true;
            }
            catch (UnauthorizedException)
            {
                return new HttpResponseMessage(HttpStatusCode.Unauthorized);
            }
            catch (Exception exception)
            {
                response.Error = String.Format("exception message: {0} \ninner exception: {1} \nstacktrace: {2}",
                                               exception.Message, exception.InnerException, exception.StackTrace);
            }
            return response;
        }
Esempio n. 4
0
        public void UpdateCuratorPerson(long curatorId, long personId, Person person)
        {
            using (var personRepository = new BaseRepository<Person>())
            {
                Client curator = personRepository.Context.Clients.FirstOrDefault(x => x.Id == curatorId);
                if (curator == null)
                {
                    throw new UserDoesNotExistException();
                }

                Person foundedPerson = personRepository.GetAllItems.FirstOrDefault(x => x.Id == personId && x.Curator.Id == curatorId);
                if (foundedPerson == null)
                {
                    throw new PersonDoesNotExistException();
                }

                // All fields can be updated except CuratorId!!!!!

                if (person.PersonsGroup != null)
                {
                    PersonsGroup personsGroup =
                    personRepository.Context.PersonsGroups.FirstOrDefault(x => x.Id == person.PersonsGroup.Id);
                    if (person.PersonsGroup.Id != 0 && personsGroup == null)
                    {
                        throw new PersonsGroupDoesNotExistException();
                    }

                    if (person.PersonsGroup.Id == 0)
                    {
                        PersonsGroup groupRelated = foundedPerson.PersonsGroup;
                        if (groupRelated != null)
                        {
                            groupRelated.Persons.Remove(foundedPerson);
                            foundedPerson.PersonsGroup = null;
                            personRepository.Context.SaveChanges();
                        }

                    }
                    else
                    {
                        foundedPerson.PersonsGroup = personsGroup;
                    }
                }

                // If email updated and not equals current - check unique
                // select Email from Persons where Curator_Id = curatorId
                if (!String.IsNullOrWhiteSpace(person.Email) && !String.Equals(person.Email, foundedPerson.Email, StringComparison.OrdinalIgnoreCase))
                {
                    if (personRepository.GetAllItems.Where(x => x.Curator.Id == curatorId).Select(x => x.Email).Contains(person.Email))
                    {
                        throw new PersonEmailException();
                    }
                    foundedPerson.Email = person.Email;
                }

                if (person.FirstName != null)
                {
                    foundedPerson.FirstName = person.FirstName;
                }
                if (person.LastName != null)
                {
                    foundedPerson.LastName = person.LastName;
                }
                if (person.MiddleName != null)
                {
                    foundedPerson.MiddleName = person.MiddleName;
                }
                if (person.BirthDate.HasValue)
                {
                    foundedPerson.BirthDate = person.BirthDate;
                }
                if (person.Note != null)
                {
                    foundedPerson.Note = person.Note;
                }

                foundedPerson.Gender = person.Gender;

                if (!personRepository.Update(foundedPerson).Status)
                {
                    throw new UpdateException();
                }
            }
        }