/// <summary> /// Saves the wish. /// </summary> /// <param name="wish">The saved wish.</param> /// <returns></returns> public Wish SaveWish(Wish wish) { var dataContext = GetWriteDataContext(); wish.Name = TruncateString(wish.Name, 100); wish.Description = TruncateString(wish.Description, 500); wish.LinkUrl = TruncateString(wish.LinkUrl, 255); DateTime timeStamp = DateTime.Now; var wishToSave = (from w in dataContext.Wishes where w.WishId == wish.Id select w).SingleOrDefault(); bool createNew = wishToSave == null; if (createNew) { wishToSave = new RepData.Wish(); dataContext.Wishes.InsertOnSubmit(wishToSave); wishToSave.Created = timeStamp; wishToSave.Changed = timeStamp; } else if (!wish.CalledDiffers(wishToSave.TjingedById)) { //We don't want to update the change time if the wish was called or uncalled wishToSave.Changed = timeStamp; } wishToSave.Name = wish.Name; wishToSave.Description = wish.Description; wishToSave.LinkUrl = wish.LinkUrl; wishToSave.TjingedById = wish.CalledByUser != null ? (int?)wish.CalledByUser.Id : null; wishToSave.OwnerId = wish.Owner.Id; dataContext.SubmitChanges(); Wish savedWish = (Wish)wish.Clone(); savedWish.Id = wishToSave.WishId; if (createNew) { savedWish.Created = wishToSave.Created; } savedWish.Changed = wishToSave.Changed; return(savedWish); }
public Wish SaveWish(Wish wish) { if (wish.Id < 1) { wish.Id = wishes.Count + 1; wish.Created = DateTime.Now; wish.Changed = wish.Created; wishes.Add(wish); return(wish); } else { Wish wishToUpdate = (from w in wishes where w.Id == wish.Id select w).SingleOrDefault <Wish>(); if (wishToUpdate != null) { //Don't update change date if the wish was called if (!wish.CalledDiffers(wishToUpdate.CalledByUser != null ? (int?)wishToUpdate.CalledByUser.Id : null)) { wishToUpdate.Changed = DateTime.Now; } wishToUpdate.Description = wish.Description; wishToUpdate.LinkUrl = wish.LinkUrl; wishToUpdate.Name = wish.Name; wishToUpdate.Owner = wish.Owner; wishToUpdate.CalledByUser = wish.CalledByUser; return(wishToUpdate); } else { throw new InvalidOperationException("Could not update wish - did not exist in repository"); } } }