public async Task <IActionResult> UpdateActivityRules([FromBody] CreateCountryActivityRule activityRules) { try { if (!ModelState.IsValid) { return(Json(new { IsSuccess = false, Message = "" })); } else { var country = await _countryService.GetCountryById(activityRules.CountryId); if (country is null) { return(Json(new { IsSuccess = false, Message = "Invalid country specified" })); } if (country.OwnerId != _userAppContext.CurrentUserId) { return(Json(new { IsSuccess = false, Message = "Not the owner of the country" })); } var isUpdated = _countryService.UpdateCountryActivityRules(activityRules); return(Json(new { IsSuccess = isUpdated })); } } catch (Exception e) { return(Json(new { IsSuccess = false, Message = e.Message })); } }
public bool UpdateCountryActivityRules(CreateCountryActivityRule activityRules) { try { var currentRules = _countryRuleRepository .Get(x => x.CountryId == activityRules.CountryId && !x.IsDeleted) .SingleOrDefault(); if (currentRules is object) { currentRules.IsDeleted = true; _countryRuleRepository.Update(currentRules); } var newRules = new CountryActivityRule { Id = ObjectId.GenerateNewId().ToString(), CountryId = activityRules.CountryId, Article = activityRules.Article, ArticleLike = activityRules.ArticleLike, ArticleView = activityRules.ArticleView, Story = activityRules.Story, StoryLike = activityRules.StoryLike, StoryView = activityRules.StoryView, Comment = activityRules.Comment, CommentLike = activityRules.CommentLike, Share = activityRules.Share, Referral = activityRules.Referral, }; _countryRuleRepository.Add(newRules); return(true); } catch (Exception e) { throw new ApplicationException("Unable to update country activity rules: " + e.Message); } }