public async Task <int> Handle(AddPersonProjectCommand request, CancellationToken cancellationToken)
        {
            PersonProject personProject;

            if (request.Id > 0)
            {
                personProject = await this.context.PersonProject.SingleOrDefaultAsync(pp => pp.Id == request.Id);

                personProject.Amount    = request.Amount;
                personProject.StartDate = request.StartDate;
            }
            else
            {
                personProject = HMapper.Mapper.Map <AddPersonProjectCommand, PersonProject>(request);
                await this.context.PersonProject.AddAsync(personProject);

                await this.mediator.Send(new UpdateProjectStartDateCommand()
                {
                    ProjectId = request.ProjectId, StartDate = request.StartDate
                });
            }

            await context.SaveChangesAsync();

            return(personProject.Id);
        }
Exemplo n.º 2
0
        public async Task <Unit> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            // validation
            if (string.IsNullOrWhiteSpace(request.Password))
            {
                throw new AppException("Mot de passe requis.");
            }

            if (context.Person.Any(x => x.UserName == request.UserName))
            {
                throw new AppException("Le login \"" + request.UserName + "\" est déjà pris.");
            }

            byte[] passwordHash, passwordSalt;
            this.passwordService.CreatePasswordHash(request.Password, out passwordHash, out passwordSalt);

            var person = HMapper.Mapper.Map <RegisterCommand, Person>(request);

            person.CreatedOn    = DateTime.Now;
            person.UpdatedOn    = DateTime.Now;
            person.PasswordHash = passwordHash;
            person.PasswordSalt = passwordSalt;

            await context.Person.AddAsync(person);

            await context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemplo n.º 3
0
        public async Task <Unit> Handle(RemovePersonProjectCommand request, CancellationToken cancellationToken)
        {
            PersonProject personProject = await this.context.PersonProject.FindAsync(request.Id);

            context.PersonProject.Remove(personProject);
            await context.SaveChangesAsync();

            return(Unit.Value);
        }
Exemplo n.º 4
0
        public async Task <Unit> Handle(UpdatePersonCommand request, CancellationToken cancellationToken)
        {
            var person = await context.Person.FindAsync(request.Id);

            if (person == null)
            {
                throw new AppException("Utilisateur non trouvé.");
            }

            if (request.UserName != person.UserName)
            {
                // username has changed so check if the new username is already taken
                if (context.Person.Any(x => x.UserName == request.UserName))
                {
                    throw new AppException("Le Login " + request.UserName + " est déjà pris.");
                }
            }

            // update user properties
            HMapper.Mapper.Fill(request, person);
            person.UpdatedOn = DateTime.Now;
            // update password if it was entered
            if (!string.IsNullOrWhiteSpace(request.Password))
            {
                byte[] passwordHash, passwordSalt;
                this.passwordService.CreatePasswordHash(request.Password, out passwordHash, out passwordSalt);

                person.PasswordHash = passwordHash;
                person.PasswordSalt = passwordSalt;
            }

            context.Person.Update(person);
            await context.SaveChangesAsync();

            return(Unit.Value);
        }