private List <KeyValuePair <string, string> > ValidateOfferModel(AddOfferModel model) { var context = new BloodSearchContext(); var ret = new List <KeyValuePair <string, string> >(); if (model.Id < 0) { ret.Add(new KeyValuePair <string, string>("offer-id-out-of-range", "неверное значение offerId")); } if (model.Offer == null) { ret.Add(new KeyValuePair <string, string>("offer-is-empty", "offer не может быть пустым")); } if (model.Id > 0) { var offer = context.Offers.AsNoTracking().FirstOrDefault(x => x.Id == model.Id); if (offer == null) { ret.Add(new KeyValuePair <string, string>("can't-find-offer", $"не найдено объявления id = {model.Id}")); } } return(ret); }
public static AddOfferResult AddOfferSync(AddOfferModel model) { var url = $"{SchemeAndHost}/api/offers/add-offer"; var jData = JsonConvert.SerializeObject(model); return(ApiProvider.ExecutePostSync <AddOfferResult>(url, jData)); }
private Offer SaveOffer(AddOfferModel model) { var context = new BloodSearchContext(); Offer offer = null; if (model.Id.HasValue && model.Id > 0) { offer = context.Offers.FirstOrDefault(x => x.Id == model.Id); } if (offer == null) { offer = new Offer { CreatedDate = DateTime.Now, ChangedDate = DateTime.Now, State = OfferStateEnum.New, Type = model.Type, UserId = model.UserId }; } if (offer.Id == 0) { context.Offers.Add(offer); } var jOfferModel = JsonConvert.SerializeObject(model.Offer); if (offer.JData != jOfferModel) { offer.JData = jOfferModel; offer.RowProcessingStatus = RowProcessingStatusEnum.New; offer.RowChangedDate = DateTime.Now; offer.ChangedDate = DateTime.Now; } if (offer.RowProcessingStatus != RowProcessingStatusEnum.New) { offer.RowProcessingStatus = RowProcessingStatusEnum.New; offer.RowChangedDate = DateTime.Now; offer.ChangedDate = DateTime.Now; } if (offer.UserId != model.UserId) { offer.UserId = model.UserId; offer.RowChangedDate = DateTime.Now; offer.ChangedDate = DateTime.Now; } context.SaveChanges(); context.Entry(offer).State = System.Data.Entity.EntityState.Detached; return(offer); }
public static Offer Map_AddServiceOfferModel_To_Offer(AddOfferModel model, string authorID) { return(new Offer { AuthorId = authorID, Description = model.ManageOffers.Description, Title = model.ManageOffers.Title, RatePerHour = model.ManageOffers.RatePerHour, Category = model.ManageOffers.Category, Subcategory = model.ManageOffers.Subcategory, }); }
public async Task <ActionResult> Add(AddOfferModel model) { bool result = await _offerProxy.CreateServiceOfferAsync( Mapping.Mapping.Map_AddServiceOfferModel_To_Offer(model, User.Identity.GetUserId())); if (result) { return(RedirectToAction("Index")); } return(View("Add", model)); }
public AddOfferResult AddOffer(AddOfferModel model) { var ret = new AddOfferResult { ErrMessages = ValidateOfferModel(model) }; if (ret.ErrMessages.Any()) { ret.Success = false; return(ret); } var offer = Retry.Do(() => SaveOffer(model)); ret.Id = offer.Id; ret.Success = true; return(ret); }
internal object AddOffer(AddOfferModel offerModel) { try { Offer new_offer = new Offer { UserId = offerModel.UserId, OfferDescription = offerModel.OfferDescription, OfferImage = ParseImage(offerModel.OfferImage), ValidDate = offerModel.ValidDate, PostedDate = DateTime.Now }; _dbContext.Offers.Add(new_offer); _dbContext.SaveChanges(); return("{Success: true}"); } catch (Exception ex) { return("{Success: " + ex.Message + "}"); } }
public void Add_View_Tests_If_Rederict_to_Action() { try { var controllerContext = new Mock <ControllerContext>(); var principal = new Moq.Mock <IPrincipal>(); principal.SetupGet(x => x.Identity.Name).Returns("uname"); //principal.SetupGet(x => x.Identity.GetUserId()).Returns("uname"); controllerContext.SetupGet(x => x.HttpContext.User).Returns(principal.Object); WorkingHoursOfOfferModel[] array = { new WorkingHoursOfOfferModel() }; AddOfferModel model = new AddOfferModel(); ManageOfferModel manageOffers = new ManageOfferModel(); manageOffers.Author = "2"; manageOffers.Category = Category.Architecture; manageOffers.Description = "2222222222222222"; manageOffers.Id = 22; manageOffers.RatePerHour = 111; manageOffers.Subcategory = SubCategory.InteriorDesign; manageOffers.Title = "my titlee"; model.ManageOffers = manageOffers; model.WorkingDays = array.AsEnumerable <WorkingHoursOfOfferModel>(); var serviceMock = new Mock <MyWeb.OfferReference.IOfferService>(); serviceMock.Setup(x => x.CreateServiceOfferAsync(It.IsAny <Offer>())).ReturnsAsync(true); var ctr = new ServiceOfferController(serviceMock.Object); ctr.ControllerContext = controllerContext.Object; var task = ctr.Add(model);//Task<actionRsult>> var result = task.Result; Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult routeResult = result as RedirectToRouteResult; Assert.AreEqual(routeResult.RouteValues["action"], "Index"); } catch { Assert.Fail(); } }
public ActionResult Add(AddOfferModel model, HttpPostedFileBase offerimage) { try { //Explicitly Validate Model for OfferType, PublishVia and offerimage if ((model.Type != OfferBase.OfferType.DiscountAmount && model.Type != OfferBase.OfferType.DiscountPercent && model.Type != OfferBase.OfferType.FreeServing) || (model.PublishedBy != AddOfferModel.PublishedVia.Advertisement && model.PublishedBy != AddOfferModel.PublishedVia.Code)) { ModelState.AddModelError("addstatus", "An Offer needs to be one of the Offer Types and should have one of the Publish Medium"); } if (model.PublishedBy == AddOfferModel.PublishedVia.Advertisement && (offerimage == null || offerimage.ContentLength < 1 || offerimage.ContentType != "image/jpeg" && offerimage.ContentType != "image/png")) { ModelState.AddModelError("addstatus", "An Offer with Advertisement needs to have a valid Advertisement Image, Only JPEG and PNG images are supported"); } if (model.PublishedBy == AddOfferModel.PublishedVia.Code && (model.CouponCode == null || model.CouponCode.Trim().Length < 1)) { ModelState.AddModelError("addstatus", "An Offer with Code needs to have a valid Coupon Code"); } if (ModelState.IsValid) { // Attempt to add the offer var restaurantoffer = model.PublishedBy == AddOfferModel.PublishedVia.Advertisement ? new SeasonalOffer { Type = model.Type, Description = model.Description, Title = model.Title, ValidFrom = model.ValidFrom, ValidTill = model.ValidTill, Value = model.Value, PictureFileName = new ImagesController().PutImage(offerimage, null). ToString("n") } : new Coupon(model.CouponCode) { Type = model.Type, Description = model.Description, Title = model.Title, ValidFrom = model.ValidFrom, ValidTill = model.ValidTill, Value = model.Value } as OfferBase; var offerid = Repository.Add(restaurantoffer); if (offerid > 0) { TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification { Message = String.Format("Offer Id:{0}, {1} was successfully Added as a new {2}", offerid, model.Title, model.PublishedBy == AddOfferModel.PublishedVia.Code ? "Coupon" : "Seasonal Offer"), Result = true, State = ActionResultNotification.MessageState.Information }; return(RedirectToAction("Index")); } } // If we got this far, something failed, redisplay form TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification { Message = ModelState.ContainsKey("addstatus") ? ModelState["addstatus"].Errors[0].ErrorMessage : "There was an Error in adding the new Offer, please try again", Result = false, State = ActionResultNotification.MessageState.Error }; return(View(model)); } catch (Exception e) { TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification { Message = e.Message, Result = false, State = ActionResultNotification.MessageState.Error }; return(View(model)); } }
public IActionResult Post(AddOfferModel offerModels) { return(Ok(offerService.AddOffer(offerModels))); }