public HttpResponseMessage UpdateChoreUser(string AuthToken, int Id, string ChoreUserValues) { try { _User User = new _User(UserRepository.RefreshAuthToken(AuthToken)); ChoreUser ChoreUserObject = JsonConvert.DeserializeObject <ChoreUser>(ChoreUserValues); //get chorelist from chore user var ChoreList = ChoreRepository.GetChoreList(ChoreUserObject.ChoreListId); //check if userid matches chore list object user if (User.Id != ChoreList.UserId) { throw new Exception("Unathorized"); } //keep chore user as active ChoreUserObject.IsActive = true; return(OKResponse(new _ChoreUser(ChoreRepository.UpdateChoreUser(Id, ChoreUserObject)))); } catch (Exception ex) { return(ErrorResponse(ex)); } }
public ChoreUser UpdateChoreUser(int Id, ChoreUser NewChoreUser) { try { var OldChoreUser = db.ChoreUsers.SingleOrDefault(x => x.Id == Id); //iterate through all properties //except Id foreach (var property in NewChoreUser .GetType() .GetProperties() .Where(x => x.Name != "Id" && x.Name != "SortOrder")) { //get the value of the iterated property var value = property.GetValue(NewChoreUser); if (value != null) { Type type = NewChoreUser.GetType(); PropertyInfo propertyInfo = type.GetProperty(property.Name); propertyInfo.SetValue(OldChoreUser, value, null); } } db.SaveChanges(); return(OldChoreUser); } catch (Exception ex) { throw Utility.ThrowException(ex); } }
public ChoreUser AddChoreUser(ChoreUser Value) { try { //set sort order to last Value.SortOrder = db.ChoreUsers.Count(x => x.ChoreListId == Value.ChoreListId && x.IsActive) + 1; //add value Value = db.ChoreUsers.Add(Value); //make chore status inactive if rules broken if (!ChoreListIsValid(Value.ChoreListId)) { var ChoreList = db.ChoreLists.FirstOrDefault(x => x.Id == Value.ChoreListId); ChoreList.StatusId = 2; } db.SaveChanges(); return(Value); } catch (Exception ex) { throw Utility.ThrowException(ex); } }
public _ChoreUser(ChoreUser Value) { try { TinyMapper.Bind <ChoreUser, _ChoreUser>(); TinyMapper.Map <ChoreUser, _ChoreUser>(Value, this); } catch (Exception ex) { throw Utility.ThrowException(ex); } }
// CREATE public bool Create(ChoreUserCreate model) { var entity = new ChoreUser() { UserId = _userId.ToString(), ChoreId = model.ChoreId, }; using (var ctx = new ApplicationDbContext()) { ctx.ChoreUsers.Add(entity); return(ctx.SaveChanges() == 1); } }
// CREATE CHOREUSER WITH CHOREID public bool CreateChoreUser(ChoreUserCreate model) { using (var ctx = new ApplicationDbContext()) { var chore = GetChoreById(model.ChoreId); var entity = new ChoreUser() { UserId = _userId.ToString(), ChoreId = model.ChoreId, ChoreIsComplete = model.ChoreIsComplete }; ctx.ChoreUsers.Add(entity); return(ctx.SaveChanges() == 1); } }