public async Task <ActionResult <IEnumerable <Product> > > ListProducts() { try { if (!await User.IsPlaceProviderAdmin(userRepository, placeProviderRepository)) { throw new Exception(localizer[Resources.Controllers_AdminController.Only_admin_is_allowed_to_invite_other_users].Value); } return(Ok(await placeProviderRepository.ListProducts(User.GetPlaceProvider()))); } catch (ArgumentException exc) { logger.LogError(exc.Message); return(BadRequest(new ProblemDetails() { Detail = exc.Message })); } catch (Exception exc) { logger.LogError(exc, exc.Message); return(BadRequest(new ProblemDetails() { Detail = exc.Message })); } }
public async Task <ActionResult <PlaceProduct> > InsertOrUpdatePlaceProduct( [FromBody] PlaceProduct placeProduct ) { try { if (!await User.IsPlaceProviderAdmin(userRepository, placeProviderRepository)) { throw new Exception(localizer[Controllers_PlaceController.Only_admin_is_allowed_to_manage_testing_places].Value); } var place = await placeRepository.GetPlace(placeProduct.PlaceId); if (place == null) { throw new Exception("Place not found"); } if (place.PlaceProviderId != User.GetPlaceProvider()) { throw new Exception("You can define product only for your places"); } var update = true; if (string.IsNullOrEmpty(placeProduct.Id)) { update = false; } logger.LogInformation($"InsertOrUpdatePlaceProduct : {placeProduct.PlaceId} {update}"); var products = await placeProviderRepository.ListProducts(User.GetPlaceProvider()); var product = products.FirstOrDefault(p => p.Id == placeProduct.ProductId); if (product == null) { throw new Exception("Product not found"); } if (!update) { // new place placeProduct.Id = Guid.NewGuid().ToString(); placeProduct.PlaceProviderId = User.GetPlaceProvider(); placeProduct.InsuranceOnly = product.InsuranceOnly; placeProduct.CollectInsurance = product.CollectInsurance; placeProduct.CollectEmployeeNo = product.CollectEmployeeNo; placeProduct.CollectNationality = product.CollectNationality; placeProduct.SchoolOnly = product.SchoolOnly; placeProduct.EmployeesOnly = product.EmployeesOnly; placeProduct.EHealthDefault = product.EHealthDefault; placeProduct.EmployeesRegistration = product.EmployeesRegistration; placeProduct = await placeRepository.SetProductPlace(placeProduct); logger.LogInformation($"ProductPlace {place.Name} has been created"); } else { // update existing if (placeProduct.PlaceProviderId != User.GetPlaceProvider()) { throw new Exception("You can define place products only for your places"); } var oldPlaceProduct = await placeRepository.GetPlaceProduct(placeProduct.Id); placeProduct.InsuranceOnly = product.InsuranceOnly; placeProduct = await placeRepository.SetProductPlace(placeProduct); logger.LogInformation($"Place {place.Name} has been updated"); } return(Ok(placeProduct)); } catch (ArgumentException exc) { logger.LogError(exc.Message); return(BadRequest(new ProblemDetails() { Detail = exc.Message })); } catch (Exception exc) { logger.LogError(exc, exc.Message); return(BadRequest(new ProblemDetails() { Detail = exc.Message })); } }