private void AddTags(string[] tags, Campaign campaign) { var lengthyTags = tags.Where(t => t.Length > 20); if (lengthyTags.Count() > 0) { CustomHttpExceptions.CustomBadRequest("Some tags are lengthy:" + string.Join(",", lengthyTags)); } //Sanitize tags tags = tags.Select(t => Helpers.MySanitizer.StrictSanitize(t)).ToArray(); /*NOTE:AddRange is a no-op if the entity is already in the context in the Added state, but the tags added * previously are in Unchanged state so we have to add only new entities*/ var oldTags = campaign.TagMaps.ToList(); var newTags = tags.Select(t => new CampaignTag { Name = t }).ToList(); //Remove only old tags that are not in new tags foreach (var t in oldTags) { if (!tags.Contains(t.CampaignTagName)) { db.CampaignTagMaps.Remove(t); } } //add only new tags that are not in old tags var oldTagNames = oldTags.Select(ot => ot.CampaignTagName).ToArray(); foreach (var nt in newTags) { if (!TagExist(nt)) { db.CampaignTags.Add(nt); } if (!oldTagNames.Contains(nt.Name)) { db.CampaignTagMaps.Add(new CampaignTagMap { CampaignTagName = nt.Name, CampaignId = campaign.Id }); } } }
public bool CheckandUpdateWaitingStatus(Campaign campaign, string status) { if (string.Equals(status, CampaignStatus.Waiting.ToString(), StringComparison.OrdinalIgnoreCase)) { /* * NOTE: This is not neccessary and in some cases against RESTful implementation * although not practical from client-side point of view there are no problems with changing * some campaign props and request a waiting status at the same request! * //counts non-null values of model * var nonNullCount = typeof(UpdateCampaignVM).GetProperties() * .Where(p => p.GetValue(model) != null) * .Count(); * if (nonNullCount > 1) { * CustomBadRequest(string.Format( * "Model can not have Status value and other values at the same time: {0} non-null values in model" * ,nonNullCount)); * } */ ModelState.Clear(); var validationProps = typeof(Campaign_WaitingValidationModel).GetProperties().Select(p => p.Name).ToList(); //Check Null or Empty values of campaign var nullOrWhiteSpaceProps = typeof(Campaign).GetProperties() .Where(p => validationProps.Contains(p.Name)) .Where(p => string.IsNullOrWhiteSpace((p.GetValue(campaign) ?? "").ToString())) .Select(p => p.Name); if (nullOrWhiteSpaceProps.Count() > 0) { CustomHttpExceptions.CustomBadRequest("The following properties of campaign have Null or Empty values:" + string.Join(",", nullOrWhiteSpaceProps.ToArray())); } return(true); } return(false); }