public ActionResult OrderReview(OrderReviewViewModel model) { if (model.pubId > 0) { for (int i = 0; i < model.Rewiever.Count(); i++) { if (model.Rewiever[i] == true) { Review review = new Review(); ReviewStateHistory rsh = new ReviewStateHistory(); DataContext cont = new DataContext(); IUserService userService = new UserService(cont); IReviewService reviewService = new ReviewService(cont); User targetUser = userService.GetOwinUser(model.ValidUsers[i].Id); var state = reviewService.GetState("Przydzielony"); review.User = targetUser; DateTime date = DateTime.UtcNow; review.Creation_date = date; review.PublicationId = model.pubId; review.CurrentState = state; targetUser.Reviews.Add(review); reviewService.CreateReview(review, rsh, state); return(RedirectToAction("Index")); } } } return(Redirect("PublicationList")); }
public void CreateReview(Review _review, ReviewStateHistory _newhistory, ReviewState _state) { _review.Creation_date = DateTime.UtcNow; _review.Expiration_date = DateTime.UtcNow.AddYears(2); ctx.Reviews.Add(_review); ctx.SaveChanges(); _newhistory.Review = _review; _newhistory.StateId = _state.Id; _newhistory.State = _state; _newhistory.ChangeDate = DateTime.UtcNow; ctx.ReviewStateHistory.Add(_newhistory); ctx.SaveChanges(); }
public ActionResult AddReview(ReviewViewModel model) { if (ModelState.IsValid && model.File != null && model.pubId > 0) { using (DataContext ctx = new DataContext()) { //Review review = new Review(); IUserService userService = new UserService(); string userId = User.Identity.GetUserId(); User currentUser = ctx.Users.Where(u => u.Id == userId).FirstOrDefault(); var revId = from Review rev in ctx.Reviews where rev.PublicationId == model.pubId && rev.UserId == userId select rev.ReviewId; int reviewId = revId.First(); Review review = ctx.Reviews.Find(reviewId); Publication pub = ctx.Publications.Find(model.pubId); //ReviewState state = ctx.ReviewStates.Where(s => s.Name == "Przydzielony").FirstOrDefault(); //review.CurrentState = state; //File string filePath = Server.MapPath("~/Reviews/"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileName = model.File.FileName; fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1); model.File.SaveAs(filePath + fileName); Data.Model.File file = new Data.Model.File() { Name = fileName, Link_source = filePath + fileName, IsCurrent = true, Review = review }; ctx.Files.Add(file); //var original = ctx.Reviews.Find(review); if (review != null) { //review.User = currentUser; //DateTime date = DateTime.Now; //review.Creation_date = date; //date = date.AddDays(14); //review.Expiration_date = date; //review.PublicationId = model.pubId; //review.Publication = pub; review.Files.Add(file); review.CurrentStateId = 2; //ctx.SaveChanges(); } //currentUser.Reviews.Add(review); //ctx.Reviews.Add(review); var fId = from Data.Model.File f in ctx.Files where f.ReviewId == reviewId select f.FileId; if (fId.FirstOrDefault() != default(int)) { int fileId = fId.First(); Data.Model.File fileCheck = ctx.Files.Find(fileId); if (fileCheck != null) { ReviewStateHistory stateHistory = new ReviewStateHistory(); stateHistory.ChangeDate = DateTime.Now; stateHistory.StateId = review.CurrentStateId; stateHistory.ReviewId = reviewId; ctx.ReviewStateHistory.Add(stateHistory); } } var fileOldId = from Data.Model.File f in ctx.Files where f.ReviewId == reviewId && f.IsCurrent == true select f.FileId; if (fileOldId.FirstOrDefault() != default(int)) { Data.Model.File fileOld = ctx.Files.Find(fileOldId.First()); fileOld.IsCurrent = false; } review.Publication = pub; pub.Reviews.Add(review); //Tu moze byc problem //ctx.Entry(pub).State = EntityState.Modified; //ctx.Entry(pub.Reviews).State = EntityState.Modified; ctx.SaveChanges(); return(RedirectToAction("Index")); } } return(View(model)); }