//[ValidationActionFilter] public string Create([FromBody] CreateCarouselModelNoFile model) { if (ModelState.IsValid) { int seq = 0; if (model.Sequence == 0) { var record = db.Carousel.FirstOrDefault(c => !c.IsDeleted); if (record != null) { seq = db.Carousel.Where(c => !c.IsDeleted).Max(x => x.Sequence); } } seq++; var carousel = new Carousel { Title = model.Title, Description = model.Description, Sequence = seq, Display = (model.Display) ? true : false, DisplayDate = model.DisplayDate, IsDeleted = false, CreatedDate = DateTime.Now, CreatedBy = model.CreatedBy, TextLocation = (int)model.TextLocation, FreeTextArea = model.FreeTextArea }; db.Carousel.Add(carousel); db.SaveChanges(); //files 1 foreach (var fileid in model.CoverFilesId) { var coverfile = new CarouselFile { FileId = fileid, ParentId = carousel.Id }; db.CarouselFile.Add(coverfile); } // modify carousel by adding ref no based on year, month and new ID (Survey= SVP & SVT) var refno = "CRS/" + DateTime.Now.ToString("yyMM"); refno += "/" + carousel.Id.ToString("D4"); carousel.RefNo = refno; db.Entry(carousel).State = EntityState.Modified; db.SaveChanges(); return(carousel.Id.ToString() + "|" + model.Title); } return(""); }
public string Edit([FromBody] CreateCarouselModel model) { if (ModelState.IsValid) { var carousel = db.Carousel.Where(p => p.Id == model.Id).FirstOrDefault(); if (carousel != null) { carousel.Title = model.Title; carousel.Description = model.Description; carousel.Display = model.Display; carousel.DisplayDate = model.DisplayDate; carousel.TextLocation = (int)model.TextLocation; carousel.FreeTextArea = model.FreeTextArea; carousel.LastModifiedBy = model.LastModifiedBy; carousel.LastModifiedDate = DateTime.Now; db.Entry(carousel).State = EntityState.Modified; //files 1 var attachments1 = db.CarouselFile.Where(s => s.ParentId == model.Id).ToList(); if (attachments1 != null) { if (model.CoverPictures == null) { foreach (var attachment in attachments1) { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.CarouselFile.Remove(attachment); } } else { foreach (var attachment in attachments1) { if (!model.CoverPictures.Any(u => u.Id == attachment.FileDocument.Id)) { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.CarouselFile.Remove(attachment); } } } } foreach (var fileid in model.CoverFilesId) { var coverfile = new CarouselFile { FileId = fileid, ParentId = carousel.Id }; db.CarouselFile.Add(coverfile); } db.SaveChanges(); return(model.Title); } } return(""); }