public Worker CreateWorker(CreateWorkerRequest createWorkerRequest) { if (GetWorkerByEmail(createWorkerRequest.EmailAddress) != null) { throw new PostWorkerException($"Worker with Email {createWorkerRequest.EmailAddress} already exists"); } var worker = new Worker { Role = createWorkerRequest.Role, Email = createWorkerRequest.EmailAddress, FirstName = createWorkerRequest.FirstName, LastName = createWorkerRequest.LastName, IsActive = true, CreatedBy = createWorkerRequest.CreatedBy, ContextFlag = createWorkerRequest.ContextFlag, DateStart = createWorkerRequest.DateStart, DateEnd = null, LastModifiedBy = createWorkerRequest.CreatedBy }; var workerTeams = GetTeams(createWorkerRequest.Teams); worker.WorkerTeams = new List <WorkerTeam>(); foreach (var team in workerTeams) { worker.WorkerTeams.Add(new WorkerTeam { Team = team, Worker = worker }); } _databaseContext.Workers.Add(worker); _databaseContext.SaveChanges(); return(worker); }
public IActionResult CreateWorker([FromBody] CreateWorkerRequest request) { var validator = new CreateWorkerRequestValidator(); var validationResults = validator.Validate(request); if (!validationResults.IsValid) { return(BadRequest(validationResults.ToString())); } try { var createdWorker = _workersUseCase.ExecutePost(request); return(CreatedAtAction(nameof(CreateWorker), createdWorker)); } catch (PostWorkerException e) { return(UnprocessableEntity(e.Message)); } }
public WorkerResponse ExecutePost(CreateWorkerRequest createWorkerRequest) { return(_databaseGateway.CreateWorker(createWorkerRequest).ToDomain(true).ToResponse()); }