public ResultDTO CheckItemOut(ItemDTO itemDto) { ResultDTO retValue = new ResultDTO(); using (var context = new DataAccessContext()) { var authResult = itemDto.Authenticate(context); if (!authResult.StatusCodeSuccess) { return(authResult); } Item item = (from n in context.Items where n.ID == itemDto.ID select n).First(); item.CheckOutDate = DateTime.Now; item.RequestDate = null; try { context.SaveChanges(); } catch (Exception ex) { retValue.StatusCode = (int)HttpStatusCode.InternalServerError; retValue.StatusCodeSuccess = false; retValue.StatusMessage = ex.Message; } } return(retValue); }
public ResultDTO SaveItem(ItemDTO item) { ResultDTO result = new ResultDTO(); Item itemToProcess = null; try { using (var context = new DataAccessContext()) { var authResult = item.Authenticate(context); if (!authResult.StatusCodeSuccess) { return(authResult); } if (item.ID > 0) { itemToProcess = (from n in context.Items where n.ID == item.ID select n).First(); itemToProcess.Barcode = item.Barcode; itemToProcess.CategoryId = item.CategoryId; itemToProcess.Description = item.Description; itemToProcess.ImageName = item.ImageName; itemToProcess.LocationId = item.LocationId; itemToProcess.Name = item.Name; itemToProcess.SubCategoryId = item.SubCategoryId; itemToProcess.SubLocationId = item.SubLocationId; result.StatusMessage = "Found 1 item to edit"; // remove existing many to many values var ageGroups = (from n in context.Item2AgeGroup where n.ItemId == itemToProcess.ID select n).ToList(); ageGroups.ForEach(n => context.Item2AgeGroup.Remove(n)); var subjects = (from n in context.Item2Subject where n.ItemId == itemToProcess.ID select n).ToList(); subjects.ForEach(n => context.Item2Subject.Remove(n)); } else { itemToProcess = new Item { Barcode = item.Barcode, CategoryId = item.CategoryId, Description = item.Description, ImageName = item.ImageName, LocationId = item.LocationId, Name = item.Name, SubCategoryId = item.SubCategoryId, SubLocationId = item.SubLocationId }; context.Items.Add(itemToProcess); result.StatusMessage = "Inserted 1 item"; context.SaveChanges(); } // add many to many values foreach (var ageGroup in item.Item2AgeGroup) { itemToProcess.Item2AgeGroup.Add(new Item2AgeGroup { AgeGroupId = ageGroup.AgeGroupId, ItemId = itemToProcess.ID }); } foreach (var subject in item.Item2Subject) { itemToProcess.Item2Subject.Add(new Item2Subject { SubjectId = subject.SubjectId, ItemId = itemToProcess.ID }); } context.SaveChanges(); } } catch (Exception ex) { result.StatusCode = (int)HttpStatusCode.InternalServerError; result.StatusCodeSuccess = false; result.StatusMessage = ex.ToString(); } return(result); }