public IActionResult InsertOffer(OfferInsertDto offerInsert) { try { ParamValidator validator = new ParamValidator(); validator.ValidateNull(offerInsert.FromDate, General.Messages_.NullInputMessages_.GeneralNullMessage("از تاریخ")) .ValidateNull(offerInsert.ToDate, General.Messages_.NullInputMessages_.GeneralNullMessage("تا تاریخ")) .ValidateNull(offerInsert.Name, General.Messages_.NullInputMessages_.GeneralNullMessage("عنوان")) .ValidateNull(offerInsert.Value, General.Messages_.NullInputMessages_.GeneralNullMessage("مقدار تخفیف")) .ValidateNull(offerInsert.MaximumPrice, General.Messages_.NullInputMessages_.GeneralNullMessage("حداکثر قیمت")) .ValidateNull(offerInsert.OfferTypeId, General.Messages_.NullInputMessages_.GeneralNullMessage("نوع تخفیف")) .ValidateNull(offerInsert.OfferCode, General.Messages_.NullInputMessages_.GeneralNullMessage("کد تخفیف")) .Throw(General.Results_.FieldNullErrorCode()); var offer = new Offer { Cdate = DateTime.Now.Ticks, CuserId = ClaimPrincipalFactory.GetUserId(User), Description = offerInsert.Description, FromDate = offerInsert.FromDate?.Ticks ?? 0, HaveTimer = offerInsert.HaveTimer, MaximumPrice = offerInsert.MaximumPrice, Name = offerInsert.Name, OfferCode = offerInsert.OfferCode, OfferTypeId = offerInsert.OfferTypeId, ToDate = offerInsert.ToDate?.Ticks ?? 0, Value = offerInsert.Value }; offerInsert.ProductIdList.ForEach(c => { var productOffer = new ProductOffer { Cdate = DateTime.Now.Ticks, CuserId = ClaimPrincipalFactory.GetUserId(User), ProductId = c, Value = offerInsert.Value, FromDate = offerInsert.FromDate?.Ticks ?? 0, ToDate = offerInsert.ToDate?.Ticks ?? 0, LanguageId = offerInsert.LanguageId }; offer.ProductOffer.Add(productOffer); }); _repository.Offer.Create(offer); _repository.Save(); _logger.LogData(MethodBase.GetCurrentMethod(), offer.Id, null, offerInsert); return(Ok(offer.Id)); } catch (Exception e) { _logger.LogError(e, MethodBase.GetCurrentMethod(), offerInsert); return(BadRequest(e.Message)); } }
public IActionResult UpdateOffer(OfferInsertDto offerInsert) { try { var validator = new ParamValidator(); validator.ValidateNull(offerInsert.FromDate, General.Messages_.NullInputMessages_.GeneralNullMessage("از تاریخ")) .ValidateNull(offerInsert.Id, General.Messages_.NullInputMessages_.GeneralNullMessage("آیدی")) .ValidateNull(offerInsert.ToDate, General.Messages_.NullInputMessages_.GeneralNullMessage("تا تاریخ")) .ValidateNull(offerInsert.Name, General.Messages_.NullInputMessages_.GeneralNullMessage("عنوان")) .ValidateNull(offerInsert.Value, General.Messages_.NullInputMessages_.GeneralNullMessage("مقدار تخفیف")) .ValidateNull(offerInsert.MaximumPrice, General.Messages_.NullInputMessages_.GeneralNullMessage("حداکثر قیمت")) .ValidateNull(offerInsert.OfferTypeId, General.Messages_.NullInputMessages_.GeneralNullMessage("نوع تخفیف")) .ValidateNull(offerInsert.OfferCode, General.Messages_.NullInputMessages_.GeneralNullMessage("کد تخفیف")) .Throw(General.Results_.FieldNullErrorCode()); var offer = _repository.Offer.FindByCondition(c => c.Id == offerInsert.Id).FirstOrDefault(); if (offer == null) { throw new BusinessException(XError.GetDataErrors.NotFound()); } offer.Mdate = DateTime.Now.Ticks; offer.MuserId = ClaimPrincipalFactory.GetUserId(User); offer.Description = offerInsert.Description; offer.FromDate = offerInsert.FromDate?.Ticks ?? 0; offer.HaveTimer = offerInsert.HaveTimer; offer.MaximumPrice = offerInsert.MaximumPrice; offer.Name = offerInsert.Name; offer.OfferCode = offerInsert.OfferCode; offer.OfferTypeId = offerInsert.OfferTypeId; offer.ToDate = offerInsert.ToDate?.Ticks ?? 0; offer.Value = offerInsert.Value; var deletedPRoductOffer = _repository.ProductOffer.FindByCondition(c => c.OfferId == offerInsert.Id) .ToList(); deletedPRoductOffer.ForEach(c => { _repository.ProductOffer.Delete(c); }); offerInsert.ProductIdList.ForEach(c => { var productOffer = new ProductOffer { Cdate = DateTime.Now.Ticks, CuserId = ClaimPrincipalFactory.GetUserId(User), ProductId = c, Value = offerInsert.Value, FromDate = offerInsert.FromDate?.Ticks ?? 0, ToDate = offerInsert.ToDate?.Ticks ?? 0, LanguageId = offerInsert.LanguageId }; offer.ProductOffer.Add(productOffer); }); _repository.Offer.Update(offer); _repository.Save(); _logger.LogData(MethodBase.GetCurrentMethod(), General.Results_.SuccessMessage(), null, offerInsert); return(Ok(General.Results_.SuccessMessage())); } catch (Exception e) { _logger.LogError(e, MethodBase.GetCurrentMethod(), offerInsert); return(BadRequest(e.Message)); } }