public async Task <IActionResult> PostAsync(int organizerId, int musicianId, [FromBody] SaveContractResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var factory = new FlyweightFactory(_contractService.ListByOrganizerIdAsync(organizerId).Result); var contract = _mapper.Map <SaveContractResource, Contract>(resource); var flyweight = factory.GetFlyweight(new Contract { OrganizerId = organizerId, ContractStateId = 1 }); flyweight.Operation(contract); var existingOrganizer = await _organizerService.GetByIdAsync(organizerId); if (!existingOrganizer.Success) { return(BadRequest(existingOrganizer.Message)); } var existingMusician = await _musicianService.GetByIdAsync(musicianId); if (!existingMusician.Success) { return(BadRequest(existingMusician.Message)); } contract.Organizer = existingOrganizer.Resource; contract.Musician = existingMusician.Resource; var result = await _contractService.SaveAsync(contract); if (!result.Success) { return(BadRequest(result.Message)); } var contractResource = _mapper.Map <Contract, BaseContractResourse>(result.Resource); return(Ok(contractResource)); }
public async Task <IActionResult> PostAsync([FromBody] SaveContractResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var contract = _mapper.Map <SaveContractResource, Contract>(resource); var result = await _contractService.SaveAsync(contract); if (!result.Success) { return(BadRequest(result.Message)); } var contractResource = _mapper.Map <Contract, BaseContractResourse>(result.Resource); return(Ok(contractResource)); }