public async Task <PostModelResult> Handle(PostEmployeeCommand command, CancellationToken cancellationToken)
        {
            var validationResult = await _validator.Validate(command, cancellationToken);

            if (!validationResult.Valid)
            {
                return new PostModelResult()
                       {
                           Valid   = false,
                           Message = validationResult.Message
                       }
            }
            ;

            //Do not check if Region exists beacuse it was already checked by the Validator
            var enmployee = new Employee()
            {
                FirstName = command.FirstName,
                LastName  = command.LastName,
                RegionId  = command.RegionId
            };

            await _context.Employees.AddAsync(enmployee, cancellationToken);

            var result = await _context.SaveChangesAsync(cancellationToken);

            return(new PostModelResult()
            {
                Message = result != 1 ? "Please try again!" : "Employee was created successfuly!",
                Valid = true
            });
        }
    }
示例#2
0
        public async Task <PostModelResult> Handle(PostRegionCommand command, CancellationToken cancellationToken)
        {
            var validationResult = await _validator.Validate(command, cancellationToken);

            if (!validationResult.Valid)
            {
                return new PostModelResult()
                       {
                           Valid   = false,
                           Message = validationResult.Message
                       }
            }
            ;

            var newRegion = new Region();

            newRegion.Name     = command.Name;
            newRegion.RegionId = command.RegionId;

            if (command.ParentRegionId != 0)
            {
                var parentRegion = await _context.GetRegionById(command.ParentRegionId, cancellationToken : cancellationToken);

                if (parentRegion == null)
                {
                    return(new PostModelResult()
                    {
                        Valid = false,
                        Message = $"Please give a valid parent region. Region with id {command.ParentRegionId} does not exist!"
                    });
                }

                newRegion.ParentRegion = parentRegion;
            }

            await _context.Regions.AddAsync(newRegion, cancellationToken);

            var result = await _context.SaveChangesAsync(cancellationToken);

            return(new PostModelResult()
            {
                Message = result != 1 ? "Please try again!" : "Region was created successfuly!",
                Valid = true
            });
        }
    }