public void UpdateCampaignByUpdateCampaignVM(ref Campaign campaign, Campaign_UpdateBM model) { if (model.Story != null) { try { var storyElements = JsonConvert.DeserializeObject <string[]>(model.Story); campaign.Story = JsonConvert.SerializeObject( storyElements.Select(elm => sanitizer.Sanitize(elm)) ); } catch { throw new Exception("Error in sanitizing story elements. Story might not be a deserializable Json string"); } } if (model.Title != null) { campaign.Title = Helpers.MySanitizer.StrictSanitize(model.Title); } if (model.Tagline != null) { campaign.Tagline = Helpers.MySanitizer.StrictSanitize(model.Tagline); } if (model.CategoryId != null) { campaign.CampaignCategoryId = model.CategoryId; } if (model.TotalDays != null) { campaign.TotalDays = model.TotalDays; } if (model.ProjectStageId != null) { campaign.ProjectStageId = model.ProjectStageId; } if (model.VerifiedByOrg != null) { campaign.VerifiedByOrg = model.VerifiedByOrg; } if (model.VerificationDescription != null) { campaign.VerificationDescription = model.VerificationDescription; } if (model.TargetFund != null) { campaign.TargetFund = model.TargetFund; } }
public async Task <IHttpActionResult> PostCampaign(string id_or_slug, Campaign_UpdateBM model, bool soft_delete = false) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var campaign = GetCampaignByIdOrSlug(id_or_slug); //Only the one who created the campaign can edit it //TODO: What about admins? var userId = User.Identity.GetUserId(); var user = db.Users.Find(int.Parse(userId)); if (campaign.CreatedById.ToString() != userId) { CustomHttpExceptions.CustomHttpException(HttpStatusCode.Unauthorized, string.Format( "Unauthorized: The user (Id = {0}) who has requested the update is not the creator of the campaign!", userId) ); } /*TODO: think about these conditions and code business logic accordingly: * 1) The user decides to cancel campaign in 'Waiting' status * 2) The user decides to interrupt 'Waiting' status and do some changes * 3) The user decides to remove an 'Approved' or 'Waiting' campaign */ if (campaign.Status.HasFlag(CampaignStatus.ReadOnly)) { CustomHttpExceptions.CustomHttpException(HttpStatusCode.Forbidden, "Campaign can not be modified because of its current status"); } if (soft_delete) { campaign.RemovedFlagUtc = DateTime.UtcNow; db.Entry(campaign).State = EntityState.Modified; await db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); } UpdateCampaignByUpdateCampaignVM(ref campaign, model); AddOrUpdateSlug(ref campaign); //Checks whether there is a base64 thumbnail if (model.Base64Thumbnail != null) { var uploaderResponse = await Helpers.UploadHelper.UploadBase64ImageAsync(db, userId, model.Base64Thumbnail, FileServerTokenType.CampaignImageUpload); if (uploaderResponse.StatusCode == HttpStatusCode.OK || uploaderResponse.StatusCode == HttpStatusCode.Created) { model.ThumbnailPath = uploaderResponse.FilePath; model.ThumbnailServerId = uploaderResponse.FileServerId; campaign.ThumbnailFileServerId = model.ThumbnailServerId; campaign.ThumbnailFilePath = model.ThumbnailPath; Console.WriteLine("Thumbnail Uploaded. Thumbnail Path:" + campaign.ThumbnailFilePath); } else { Console.WriteLine("Thumbnail Upload Error Code:" + uploaderResponse.StatusCode); Console.WriteLine(uploaderResponse.Message); } } if (model.CityId != null) { if (campaign.Location != null) { var location = campaign.Location; location.CityId = (int)model.CityId; db.Entry(location).State = EntityState.Modified; } else { campaign.Location = new Location { CityId = (int)model.CityId }; } } var waitingStatus = CheckandUpdateWaitingStatus(campaign, model.Status); if (waitingStatus) { campaign.Status = CampaignStatus.Waiting | CampaignStatus.ReadOnly; if (campaign.Account == null) { campaign.Account = new Account { AccountName = "cmp_" + campaign.Id.ToString(), AccountType = AccountType.CampaignAccount }; } } if (model.Tags != null) { AddTags(model.Tags, campaign); } db.Entry(campaign).State = EntityState.Modified; await db.SaveChangesAsync(); return(StatusCode(HttpStatusCode.NoContent)); }