// A simple function to amend the user details. protected CUserMod AmendDetails(CUserMod newUserData, HttpContext httpCon, List <CUserMod> theData) { CUserMod oldUserData = theData.SingleOrDefault(x => x.UId == newUserData.UId); if (oldUserData != null) { if (oldUserData.FName != newUserData.FName) { oldUserData.FName = newUserData.FName; } if (oldUserData.SName != newUserData.SName) { oldUserData.SName = newUserData.SName; } if (oldUserData.Email != newUserData.Email) { oldUserData.Email = newUserData.Email; } } httpCon.Cache[CacheKey] = theData.ToArray(); return(oldUserData); }
// Return the user intfo public bool DealWithDelete(CUserMod user) { HttpContext httpCon = HttpContext.Current; if (httpCon != null) { List <CUserMod> originalData = (( CUserMod[] )httpCon.Cache[CacheKey]).ToList(); // Ineligant way of removing and maintaining the list structure but time is not on // my side right now, however this shouldn't be costly in performance but // could be better implemented. List <CUserMod> rebuiltData = new List <CUserMod>(); int dataCount = originalData.Count(); for (int dataIter = 0; dataIter <= dataCount - 1; dataIter++) { if (user.UId != originalData[dataIter].UId) { rebuiltData.Add(originalData[dataIter]); } } httpCon.Cache[CacheKey] = rebuiltData.ToArray(); } return(true); }
// Removal public HttpResponseMessage Delete(CUserMod user) { HttpResponseMessage response; gUserRepository.DealWithDelete(user); // A little redundant (and by a little I mean completely) right now, need to finish. response = Request.CreateResponse(System.Net.HttpStatusCode.OK, "OK"); return(response); }
public bool IsUserValid(CUserMod user) { bool isValid = true; if ((user.UId == null) || (user.FName == null) || (user.SName == null) || (user.Email == null)) { isValid = false; } return(isValid); }
// A simple function used to create a user. protected bool CreateUser(CUserMod newUserData, HttpContext httpCon, List <CUserMod> theData) { // We want the date today, this should do it. DateTime date = DateTime.Today; newUserData.CrDate = date.ToShortDateString(); theData.Add(newUserData); httpCon.Cache[CacheKey] = theData.ToArray(); return(true); }
// Constructor public CUserReposit() { // We'll create some initial data at construction. HttpContext httpCon = HttpContext.Current; DateTime bruceDate = new DateTime(/*Y*/ 1939, /*M*/ 5, /*D*/ 1); DateTime clarkDate = new DateTime(/*Y*/ 1938, /*M*/ 6, /*D*/ 1); DateTime dianaDate = new DateTime(/*Y*/ 1941, /*M*/ 12, /*D*/ 1); if (httpCon != null) { if (httpCon.Cache[CacheKey] == null) { CUserMod[] users = new CUserMod[] { new CUserMod { UId = "TotallyNotBatman1939", Email = "*****@*****.**", FName = "Bruce", SName = "Wayne", CrDate = bruceDate.ToShortDateString() }, new CUserMod { UId = "supesOP", Email = "*****@*****.**", FName = "Clark", SName = "Kent", CrDate = clarkDate.ToShortDateString() }, new CUserMod { UId = "WonderWoman", Email = "*****@*****.**", FName = "Diana", SName = "Prince", CrDate = dianaDate.ToShortDateString() } }; httpCon.Cache[CacheKey] = users; } } }
// Let's get our POST on... public HttpResponseMessage Post(CUserMod user) { // This seems like an odd way of doing this I am sure, my plan was to take the CUserMod // and use it to fill in vital JS/JSon data in the test dashboard. CUserMod theUserReq = gUserRepository.DealWithPost(user); HttpResponseMessage response; if (gUserRepository.IsUserValid(theUserReq)) { response = Request.CreateResponse(System.Net.HttpStatusCode.OK, "OK"); } else { // My implementation doesn't act on it at all but here we give out an error 500. response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Invalid input"); } return(response); }
// Return the user intfo public CUserMod DealWithPost(CUserMod user) { CUserMod theUserInfo = user; bool ammend = false; CUserMod oldUserData = new CUserMod(); HttpContext httpCon = HttpContext.Current; if (httpCon != null) { List <CUserMod> theData = (( CUserMod[] )httpCon.Cache[CacheKey]).ToList(); int dataCount = theData.Count(); for (int dataIter = 0; dataIter <= dataCount - 1; dataIter++) { if (!ammend) { if (theData[dataIter].UId == user.UId) { ammend = true; oldUserData = theData[dataIter]; } } } if (ammend) { theUserInfo = AmendDetails(user, httpCon, theData); } else { CreateUser(user, httpCon, theData); } } return(theUserInfo); }