/// <summary> /// Will update the offer in the database. Sets the ModifiedDate audit column. /// </summary> /// <param name="offer">the offer to update</param> /// <returns>true if successfully updated, false otherwise</returns> public bool UpdateOffer(OfferDTO offer) { var success = false; try { if (offer != null) { var existing = context.Offer.Where(o => o.OfferId == offer.OfferId).SingleOrDefault(); if (existing != null && this.productService.Exists(offer.ProductId)) { existing.ProductId = offer.ProductId; existing.Description = offer.Description; existing.Price = offer.Price; existing.NumberOfTerms = offer.NumberOfTerms; existing.DateModified = DateTime.Now; existing.IsActive = offer.IsActive; context.SaveChanges(); success = true; } } } catch (Exception ex) { logger.LogError("Failed to add offer.", ex); success = false; } return(success); }
/// <summary> /// Will create a new offer in the database. /// </summary> /// <param name="offer">the offer to create</param> /// <returns>the id if successfully added, -1 otherwise</returns> public int CreateOffer(OfferDTO offer) { var offerId = -1; try { // Make sure the offer is passed in and has a valid foreign key before adding. if (offer != null && this.productService.Exists(offer.ProductId)) { var newOffer = new Offer() { ProductId = offer.ProductId, Description = offer.Description, Price = offer.Price, NumberOfTerms = offer.NumberOfTerms, DateCreated = DateTime.Now, DateModified = null, IsActive = offer.IsActive }; context.Offer.Add(newOffer); context.SaveChanges(); offerId = newOffer.OfferId; } } catch (Exception ex) { logger.LogError("Failed to create offer.", ex); offerId = -1; } return(offerId); }