public async Task <AddOnDTO> EditAddOn(AddOnEditDTO addOnInfo, int tripId) { using (_unitOfWork) { AddOn addOn = await _unitOfWork.AddOnRepository.GetAddOnWithVotable(addOnInfo.AddOnId); addOn.Price = addOnInfo.Price; addOn.Description = addOnInfo.Description; _unitOfWork.AddOnRepository.Update(addOn); await _unitOfWork.Save(); AddOnDTO retValue = _mapper.Map <AddOn, AddOnDTO>(addOn); await _messageControllerService.NotifyOnTripChanges(tripId, "EditAddOn", retValue); return(retValue); } }
public async Task <ActionResult> EditAddOn(int tripId, [FromBody] AddOnEditDTO addOnInfo) { try { if (!await _editRightsService.HasEditRights(tripId)) { return(BadRequest(new JsonResult("You can't currently edit this trip."))); } AddOnDTO result = await _addOnService.EditAddOn(addOnInfo, tripId); await _editRightsService.ProlongEditRights(tripId, _redisAppSettings.EditRightsProlongedTTL); if (result != null) { return(Ok(result)); } return(BadRequest(new JsonResult("Invalid add on information"))); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <bool> DeleteAddOn(int addOnId, int tripId) { using (_unitOfWork) { if (addOnId <= 0) { return(false); } AddOnDTO deletedAddOn = null; AddOn prevAddOn = null; Trip trip = await _unitOfWork.TripRepository.FindByID(tripId); AddOn currAddOn = await _unitOfWork.AddOnRepository.GetAddOnWithVotable(trip.AddOnId); while (true) { if (currAddOn.GetDecoratorId() == 0) { break; } if (currAddOn.AddOnId == addOnId) { if (prevAddOn == null) { trip.AddOnId = currAddOn.GetDecoratorId(); _unitOfWork.TripRepository.Update(trip); } else { prevAddOn.SetDecoratorId(currAddOn.GetDecoratorId()); _unitOfWork.AddOnRepository.Update(prevAddOn); } _unitOfWork.AddOnRepository.Delete(currAddOn.AddOnId); _unitOfWork.VotableRepository.Delete(currAddOn.VotableId); await _unitOfWork.Save(); deletedAddOn = _mapper.Map <AddOn, AddOnDTO>(currAddOn); break; } if (currAddOn.GetLvl1DependId() == addOnId || currAddOn.GetLvl2DependId() == addOnId) { if (prevAddOn == null) { trip.AddOnId = currAddOn.GetDecoratorId(); _unitOfWork.TripRepository.Update(trip); } else { prevAddOn.SetDecoratorId(currAddOn.GetDecoratorId()); _unitOfWork.AddOnRepository.Update(prevAddOn); } _unitOfWork.AddOnRepository.Delete(currAddOn.AddOnId); _unitOfWork.VotableRepository.Delete(currAddOn.VotableId); await _unitOfWork.Save(); } else { prevAddOn = currAddOn; } if (prevAddOn == null) { currAddOn = await _unitOfWork.AddOnRepository.GetAddOnWithVotable(trip.AddOnId); } else { currAddOn = await _unitOfWork.AddOnRepository.GetAddOnWithVotable(prevAddOn.GetDecoratorId()); } } await _messageControllerService.NotifyOnTripChanges(tripId, "RemoveAddOn", deletedAddOn); return(true); } }