public IHttpActionResult PostAddItem(NewItem item) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } GeocachesEntities db = new GeocachesEntities(); // We want to check that there is an existing geocache matching either the name or id given // Both the geocache name and id cannot be null, otherwise we cannot add it to any geocache if (item.geocacheID == null && item.geocacheName == null) { return(BadRequest("Both geocache name and geocache id cannot be null.")); } // Get a guid that either matches to geocache ID or geocache name var guidQuery = db.Geocaches.Where(g => (g.id == item.geocacheID || g.name.Equals(item.geocacheName))).Select(g => g.id); if (guidQuery.Count() > 1) { return(BadRequest("Entered a geocache id and geocache name that do not match to same geoecache.")); } Guid guid = guidQuery.FirstOrDefault(); if (guid == Guid.Empty) { return(BadRequest("Both geocache name and geocache id are not valid.")); } // Check if the name matches the requirements Regex matcher = new Regex("^([A-Za-z0-9 ]){1,50}$"); if (item.name == null || !matcher.IsMatch(item.name)) { return(BadRequest("Name must be composed of alphanumeric or space characters. Maximum length of 50.")); } // Check if name previously exists if (db.Items.Where(i => i.name.Equals(item.name)).Any()) { return(BadRequest("Item name already exists.")); } db.Items.Add(new Items() { id = Guid.NewGuid(), name = item.name, geocache_id = guid, start_time = item.startTime, end_time = item.endTime }); db.SaveChanges(); db.Dispose(); return(Ok()); }
public IHttpActionResult PostMoveItem([FromBody] MovableItem item) { if (!ModelState.IsValid) { return(BadRequest("Invalid data.")); } GeocachesEntities db = new GeocachesEntities(); // Users can identify an item either by its id or its name. Both options, however, cannot be null if (item.id == null && item.name == null) { return(BadRequest("Both item id and name cannot be null.")); } // We will use this query later, so save it. We are checking if there any items that match the name or id var query = db.Items.Where(i => (i.id == item.id || i.name.Equals(item.name))); if (!query.Any()) { return(BadRequest("Both item id and name are invalid.")); } if (query.Count() > 1) { return(BadRequest("Entered an item id and name that do not match to the same item.")); } // Active items don't have an end time if (!query.Where(i => i.end_time == null).Any()) { return(BadRequest("Only active items can be moved.")); } // We want to check that there is an existing geocache matching either the name or id given // Both the geocache name and id cannot be null, otherwise we cannot add it to any geocache if (item.geocacheName == null && item.geocacheID == null) { return(BadRequest("Both geocache name and geocache id cannot be null.")); } // Get a guid that either matches to geocache ID or geocache name var guidQuery = db.Geocaches.Where(g => (g.id == item.geocacheID || g.name.Equals(item.geocacheName))).Select(g => g.id); if (guidQuery.Count() > 1) { return(BadRequest("Entered a geocache id and geocache name that do not match to same geoecache.")); } Guid guid = guidQuery.FirstOrDefault(); if (guid == Guid.Empty) { return(BadRequest("Both geocache name and geocache id are not valid.")); } // Check how many items are in the specific geocache. If it is more than 3, then we cannot // add any more items if (db.Items.Where(i => i.geocache_id == guid).Count() >= 3) { return(BadRequest("Cannot move item to desired geocache because there are already 3+ items in there.")); } // There should only be a single result anyways bc both the id and the name must be unique var result = query.Single(); result.geocache_id = guid; db.SaveChanges(); db.Dispose(); return(Ok()); }