public HttpResponseMessage Put(CellarSubmitModel cellarItem) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); using (var session = ravenStore.OpenSession()) { UserProfile user = session.Load <UserProfile>(cellarItem.UserId); if (user != null) { if (user.CellarBottles == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } if (user.CellarBottles.Any(x => x.WineId == cellarItem.WineId)) { var item = user.CellarBottles.FirstOrDefault(x => x.WineId == cellarItem.WineId); item.FinishBottle(); if (item.BottleCount == 0) { item.Finished = true; } session.SaveChanges(); response = new HttpResponseMessage(HttpStatusCode.OK); return(response); } return(response); } } return(response); }
public HttpResponseMessage Post(CellarSubmitModel cellarItem) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound); using (var session = ravenStore.OpenSession()) { UserProfile user = session.Load <UserProfile>(cellarItem.UserId); Wine wine = session.Load <Wine>(cellarItem.WineId); if (user != null && wine != null) { if (user.CellarBottles == null) { user.CellarBottles = new List <CellarBottle>(); } if (user.CellarBottles.Any(x => x.WineId == cellarItem.WineId)) { var item = user.CellarBottles.FirstOrDefault(x => x.WineId == cellarItem.WineId); item.IncreaseBottleCount(cellarItem.Quantity); if (!string.IsNullOrEmpty(cellarItem.Notes)) { item.PersonalComment = cellarItem.Notes; } if (item.Finished) { item.Finished = false; } session.SaveChanges(); response = new HttpResponseMessage(HttpStatusCode.Created); return(response); } Winery winery = session.Load <Winery>(wine.WineryId); CellarBottle newItem = new CellarBottle(wine, winery.WineryName, cellarItem.Quantity); newItem.PersonalComment = cellarItem.Notes; user.CellarBottles.Add(newItem); session.SaveChanges(); response = new HttpResponseMessage(HttpStatusCode.Created); return(response); } } return(response); }