public async Task <IActionResult> UpdateAsync(int id, [FromBody] UpdateOfferDto request) { if (id != request.Id) { return(BadRequest()); } var command = Mapper.Map <UpdateOfferCommand>(request); return(Ok(await Mediator.Send(command))); }
public async Task <OfferDto> UpdateOffer(ulong offerId, string companyId, UpdateOfferDto updateOfferDto) { Offer offer = await _repository.GetAll().Include(o => o.OfferSkill).Where(o => o.Id == offerId).FirstOrDefaultAsync(); if (offer == null) { return(null); } if (offer.IdCompany.ToString() != companyId) { throw new AuthenticationFailedException("You are not authorized to update this offer."); } _mapper.Map(updateOfferDto, offer); // Get and check company CompanyDto offerCompany = await _companyBusiness.GetFirstOrDefault <CompanyDto>(company => company.Id == offer.IdCompany) ?? throw new ConditionFailedException("Company was not found."); // Get and check skills ICollection <SkillDto> offerSkills = null; if (updateOfferDto.Skills != null) { offer.OfferSkill.Clear(); offerSkills = await _skillBusiness.Get <SkillDto>(skill => updateOfferDto.Skills.Contains(skill.Id)); if (updateOfferDto.Skills.Count != offerSkills.Count) { throw new ConditionFailedException("One or multiple skills do not exist."); } foreach (SkillDto skillDto in offerSkills) { offer.OfferSkill.Add(new OfferSkill { Offer = offer.Id, Skill = skillDto.Id }); } } else { offerSkills = await _skillBusiness.Get <SkillDto>(skill => offer.OfferSkill.Select(os => os.Skill).Contains(skill.Id)); } offer = await _repository.Update(offer); OfferDto mappedOffer = _mapper.Map <OfferDto>(offer); mappedOffer.Skills = offerSkills; mappedOffer.Company = offerCompany; return(mappedOffer); }
public async Task <IActionResult> PutOffer(ulong id, UpdateOfferDto updateOfferDto) { try { OfferDto offerDto = await _business.UpdateOffer(id, UserId, updateOfferDto); if (offerDto == null) { return(NotFound($"An offer with id \"{id}\" was not found.")); } return(Ok(offerDto)); } catch (AuthenticationFailedException) { return(Unauthorized("You can not update this offer.")); } catch (ConditionFailedException exception) { return(BadRequest(exception.Message)); } }