protected override PhraseBeliefDto UpdateImpl(PhraseBeliefDto dto) { var results = from item in SeedData.Ton.PhraseBeliefs where item.Id == dto.Id select item; if (results.Count() == 1) { CheckContraints(dto); var phraseBeliefToUpdate = results.First(); SeedData.Ton.PhraseBeliefs.Remove(phraseBeliefToUpdate); dto.Id = Guid.NewGuid(); SeedData.Ton.PhraseBeliefs.Add(dto); UpdateReferences(phraseBeliefToUpdate, dto); return(dto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(dto.Id); } else { throw new Exceptions.VeryBadException(); } } }
//public Result<PhraseBeliefDto> New(object criteria) //{ // //throw new NotImplementedException("Ef.PhraseBeliefDal.New(object)"); // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.CreateFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Fetch(Guid id) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == id // select phraseBeliefData; // if (results.Count() == 1) // retResult = Result<PhraseBeliefDto>.Success(EfHelper.ToDto(results.First())); // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.FetchFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.FetchFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.FetchFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Update(PhraseBeliefDto dto) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == dto.Id // select phraseBeliefData; // if (results.Count() == 1) // { // var phraseBeliefDataToUpdate = results.First(); // ctx.ObjectContext.PhraseBeliefDatas.DeleteObject(phraseBeliefDataToUpdate); // var newPhraseBeliefData = EfHelper.ToData(dto); // ctx.ObjectContext.PhraseBeliefDatas.AddObject(newPhraseBeliefData); // ctx.ObjectContext.SaveChanges(); // dto.Id = newPhraseBeliefData.Id; // retResult = Result<PhraseBeliefDto>.Success(dto); // } // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.UpdateFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Insert(PhraseBeliefDto dto) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == dto.Id // select phraseBeliefData; // //SHOULD FIND ZERO LANGUAGEDTOS (NO DUPLICATE IDS, NO DUPLICATE DTOS) // if (results.Count() == 0) // { // var data = EfHelper.ToData(dto); // ctx.ObjectContext.PhraseBeliefDatas.AddObject(data); // ctx.ObjectContext.SaveChanges(); // dto.Id = data.Id; // retResult = Result<PhraseBeliefDto>.Success(dto); // } // else // { // if (results.Count() == 1) //ID ALREADY EXISTS // retResult = Result<PhraseBeliefDto>.FailureWithInfo(dto, // new Exceptions.InsertFailedException(DalResources.ErrorMsgIdNotFound)); // else //MULTIPLE IDS ALREADY EXIST?? SHOULD NOT BE POSSIBLE // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.InsertFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.InsertFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Delete(Guid id) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == id // select phraseBeliefData; // if (results.Count() == 1) // { // var phraseBeliefDataToRemove = results.First(); // ctx.ObjectContext.PhraseBeliefDatas.DeleteObject((phraseBeliefDataToRemove)); // ctx.ObjectContext.SaveChanges(); // retResult = Result<PhraseBeliefDto>.Success(EfHelper.ToDto(phraseBeliefDataToRemove)); // } // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.DeleteFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException(ex)); // } // return retResult; //} //public Result<ICollection<PhraseBeliefDto>> GetAll() //{ // var retAllPhraseBeliefDtos = new List<PhraseBeliefDto>(); // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var allPhraseBeliefDatas = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // select phraseBeliefData; // foreach (var phraseBeliefData in allPhraseBeliefDatas) // { // var phraseBeliefDto = EfHelper.ToDto(phraseBeliefData); // retAllPhraseBeliefDtos.Add(phraseBeliefDto); // } // //var allDtos = new List<PhraseBeliefDto>(ctx.ObjectContext.PhraseBeliefDatas); // return retAllPhraseBeliefDtos; // } //} protected override PhraseBeliefDto NewImpl(object criteria) { var identity = (UserIdentity)Csla.ApplicationContext.User.Identity; string currentUsername = identity.Name; Guid currentUserId = Guid.Empty; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { currentUserId = (from user in ctx.ObjectContext.UserDatas where user.Username == currentUsername select user.Id).First(); } //if ((criteria != null) && !(criteria is UserDto)) // throw new Exceptions.BadCriteriaException(DalResources.ErrorMsgBadCriteriaExceptionDetail_ExpectedTypeIsUserDto); PhraseBeliefDto newPhraseBeliefDto = new PhraseBeliefDto() { Id = Guid.NewGuid(), Text = DalResources.DefaultNewPhraseBeliefText, BelieverId = Guid.Empty, ReviewMethodId = Guid.Empty, Strength = double.Parse(DalResources.DefaultNewPhraseBeliefStrength), PhraseId = Guid.Empty, TimeStamp = DateTime.Now, UserId = currentUserId, Username = currentUsername }; return(newPhraseBeliefDto); }
protected override PhraseBeliefDto InsertImpl(PhraseBeliefDto dto) { //MAKE SURE ID ISN'T ALREADY IN DB var results = from item in SeedData.Ton.PhraseBeliefs where item.Id == dto.Id select item; if (results.Count() == 0) { CheckContraints(dto); dto.Id = Guid.NewGuid(); SeedData.Ton.PhraseBeliefs.Add(dto); //ADD PHRASEBELIEF.ID TO USER var resultsUser = from u in SeedData.Ton.Users where u.Id == dto.UserId select u; var user = resultsUser.First(); user.PhraseBeliefIds.Add(dto.Id); return dto; } else { if (results.Count() == 1) //ID ALREADY EXISTS throw new Exceptions.IdAlreadyExistsException(dto.Id); else //MULTIPLE IDS ALREADY EXIST?? throw new Exceptions.VeryBadException(); } }
protected override PhraseBeliefDto NewImpl(object criteria) { //to get to this point, we must have already been authenticated Debug.Assert(Csla.ApplicationContext.User.Identity.IsAuthenticated); if (criteria != null) { throw new ArgumentException("criteria expected to be null"); } var username = Csla.ApplicationContext.User.Identity.Name; var userId = (from u in SeedData.Ton.Users where u.Username == username select u.Id).FirstOrDefault(); if (userId == Guid.Empty) { throw new Exceptions.UserNotAuthorizedException(); } var dto = new PhraseBeliefDto() { Id = Guid.NewGuid(), Text = "", Strength = 1.0d, PhraseId = Guid.Empty, UserId = userId, Username = username }; return(dto); }
protected override PhraseBeliefDto FetchImpl(Guid id) { var currentUserId = Business.BusinessHelper.GetCurrentUserId(); using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas where phraseBeliefData.Id == id && phraseBeliefData.UserDataId == currentUserId select phraseBeliefData; if (results.Count() == 1) { var fetchedPhraseBeliefData = results.First(); PhraseBeliefDto phraseBeliefDto = EfHelper.ToDto(fetchedPhraseBeliefData); return(phraseBeliefDto); } else { if (results.Count() == 0) { throw new Exceptions.IdNotFoundException(id); } else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phraseBeliefs with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
protected override PhraseBeliefDto InsertImpl(PhraseBeliefDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { PhraseBeliefData newPhraseBeliefData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = newPhraseBeliefData.Id; return(dto); } }
public static PhraseBeliefDto ToDto(PhraseBeliefData data) { var dto = new PhraseBeliefDto() { Id = data.Id, BelieverId = data.BelieverId, PhraseId = data.PhraseDataId, ReviewMethodId = data.ReviewMethodId, Strength = data.Strength, Text = data.Text, TimeStamp = data.TimeStamp, UserId = data.UserDataId, Username = data.UserData.Username, }; return(dto); }
private void CheckContraints(PhraseBeliefDto dto) { //REFERENTIAL INTEGRITY if (dto.PhraseId == Guid.Empty || !SeedData.Ton.ContainsPhraseId(dto.PhraseId)) { throw new Exceptions.IdNotFoundException(dto.PhraseId); } if (dto.UserId == Guid.Empty || !SeedData.Ton.ContainsUserId(dto.UserId)) { throw new Exceptions.IdNotFoundException(dto.UserId); } if (string.IsNullOrEmpty(dto.Username) || !(SeedData.Ton.GetUsername(dto.UserId) == dto.Username)) { throw new ArgumentException("dto.Username"); } }
public static void LoadDataFromDto(ref PhraseBeliefData data, PhraseBeliefDto dto, LearnLanguagesContext context) { //USER INFO data.UserDataId = dto.UserId; data.UserData = EfHelper.GetUserData(dto.UserId, context); //PHRASE data.PhraseDataId = dto.PhraseId; data.PhraseData = EfHelper.GetPhraseData(dto.PhraseId, context); //SCALAR data.ReviewMethodId = dto.ReviewMethodId; data.Strength = dto.Strength; data.Text = dto.Text; data.TimeStamp = dto.TimeStamp; }
/// <summary> /// Adds the PhraseBeliefDto to the context, loading UserData and PhraseDatas into the newly /// created PhraseBeliefData. Does NOT save changes to the context. /// </summary> public static PhraseBeliefData AddToContext(PhraseBeliefDto dto, LearnLanguagesContext context) { //CREATE THE NEW OBJECT var data = context.PhraseBeliefDatas.CreateObject(); //ASSIGN PROPERTIES data.PhraseDataId = dto.PhraseId; data.ReviewMethodId = dto.ReviewMethodId; data.Strength = dto.Strength; data.BelieverId = dto.BelieverId; data.Text = dto.Text; data.TimeStamp = dto.TimeStamp; data.UserDataId = dto.UserId; //ADD OBJECT TO CONTEXT context.PhraseBeliefDatas.AddObject(data); //RETURN OBJECT return(data); }
private void UpdateReferences(PhraseBeliefDto oldPhraseBelief, PhraseBeliefDto newPhraseBelief) { ////UPDATE USERS WHO REFERENCE THIS LINE var referencedUsers = from u in SeedData.Ton.Users where u.PhraseBeliefIds.Contains(oldPhraseBelief.Id) select u; foreach (var user in referencedUsers) { user.PhraseBeliefIds.Remove(oldPhraseBelief.Id); user.PhraseBeliefIds.Add(newPhraseBelief.Id); } ////UPDATE TRANSLATIONS WHO REFERENCE THIS PHRASE //var referencedTranslations = from t in SeedData.Ton.Translations // where t.PhraseBeliefIds.Contains(oldPhraseBelief.Id) // select t; //foreach (var translation in referencedTranslations) //{ // translation.PhraseBeliefIds.Remove(oldPhraseBelief.Id); // translation.PhraseBeliefIds.Add(newPhraseBelief.Id); //} }
protected override PhraseBeliefDto InsertImpl(PhraseBeliefDto dto) { //MAKE SURE ID ISN'T ALREADY IN DB var results = from item in SeedData.Ton.PhraseBeliefs where item.Id == dto.Id select item; if (results.Count() == 0) { CheckContraints(dto); dto.Id = Guid.NewGuid(); SeedData.Ton.PhraseBeliefs.Add(dto); //ADD PHRASEBELIEF.ID TO USER var resultsUser = from u in SeedData.Ton.Users where u.Id == dto.UserId select u; var user = resultsUser.First(); user.PhraseBeliefIds.Add(dto.Id); return(dto); } else { if (results.Count() == 1) //ID ALREADY EXISTS { throw new Exceptions.IdAlreadyExistsException(dto.Id); } else //MULTIPLE IDS ALREADY EXIST?? { throw new Exceptions.VeryBadException(); } } }
//public Result<PhraseBeliefDto> New(object criteria) //{ // //throw new NotImplementedException("Ef.PhraseBeliefDal.New(object)"); // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.CreateFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Fetch(Guid id) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == id // select phraseBeliefData; // if (results.Count() == 1) // retResult = Result<PhraseBeliefDto>.Success(EfHelper.ToDto(results.First())); // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.FetchFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.FetchFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.FetchFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Update(PhraseBeliefDto dto) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == dto.Id // select phraseBeliefData; // if (results.Count() == 1) // { // var phraseBeliefDataToUpdate = results.First(); // ctx.ObjectContext.PhraseBeliefDatas.DeleteObject(phraseBeliefDataToUpdate); // var newPhraseBeliefData = EfHelper.ToData(dto); // ctx.ObjectContext.PhraseBeliefDatas.AddObject(newPhraseBeliefData); // ctx.ObjectContext.SaveChanges(); // dto.Id = newPhraseBeliefData.Id; // retResult = Result<PhraseBeliefDto>.Success(dto); // } // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.UpdateFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.UpdateFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Insert(PhraseBeliefDto dto) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == dto.Id // select phraseBeliefData; // //SHOULD FIND ZERO LANGUAGEDTOS (NO DUPLICATE IDS, NO DUPLICATE DTOS) // if (results.Count() == 0) // { // var data = EfHelper.ToData(dto); // ctx.ObjectContext.PhraseBeliefDatas.AddObject(data); // ctx.ObjectContext.SaveChanges(); // dto.Id = data.Id; // retResult = Result<PhraseBeliefDto>.Success(dto); // } // else // { // if (results.Count() == 1) //ID ALREADY EXISTS // retResult = Result<PhraseBeliefDto>.FailureWithInfo(dto, // new Exceptions.InsertFailedException(DalResources.ErrorMsgIdNotFound)); // else //MULTIPLE IDS ALREADY EXIST?? SHOULD NOT BE POSSIBLE // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.InsertFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.InsertFailedException(ex)); // } // return retResult; //} //public Result<PhraseBeliefDto> Delete(Guid id) //{ // Result<PhraseBeliefDto> retResult = Result<PhraseBeliefDto>.Undefined(null); // try // { // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // where phraseBeliefData.Id == id // select phraseBeliefData; // if (results.Count() == 1) // { // var phraseBeliefDataToRemove = results.First(); // ctx.ObjectContext.PhraseBeliefDatas.DeleteObject((phraseBeliefDataToRemove)); // ctx.ObjectContext.SaveChanges(); // retResult = Result<PhraseBeliefDto>.Success(EfHelper.ToDto(phraseBeliefDataToRemove)); // } // else // { // if (results.Count() == 0) // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, // new Exceptions.DeleteFailedException(DalResources.ErrorMsgIdNotFound)); // else // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException()); // } // } // } // catch (Exception ex) // { // retResult = Result<PhraseBeliefDto>.FailureWithInfo(null, new Exceptions.DeleteFailedException(ex)); // } // return retResult; //} //public Result<ICollection<PhraseBeliefDto>> GetAll() //{ // var retAllPhraseBeliefDtos = new List<PhraseBeliefDto>(); // using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) // { // var allPhraseBeliefDatas = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas // select phraseBeliefData; // foreach (var phraseBeliefData in allPhraseBeliefDatas) // { // var phraseBeliefDto = EfHelper.ToDto(phraseBeliefData); // retAllPhraseBeliefDtos.Add(phraseBeliefDto); // } // //var allDtos = new List<PhraseBeliefDto>(ctx.ObjectContext.PhraseBeliefDatas); // return retAllPhraseBeliefDtos; // } //} protected override PhraseBeliefDto NewImpl(object criteria) { var identity = (CustomIdentity)Csla.ApplicationContext.User.Identity; string currentUsername = identity.Name; Guid currentUserId = Guid.Empty; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { currentUserId = (from user in ctx.ObjectContext.UserDatas where user.Username == currentUsername select user.Id).First(); } //if ((criteria != null) && !(criteria is UserDto)) // throw new Exceptions.BadCriteriaException(DalResources.ErrorMsgBadCriteriaExceptionDetail_ExpectedTypeIsUserDto); PhraseBeliefDto newPhraseBeliefDto = new PhraseBeliefDto() { Id = Guid.NewGuid(), Text = DalResources.DefaultNewPhraseBeliefText, BelieverId = Guid.Empty, ReviewMethodId = Guid.Empty, Strength = double.Parse(DalResources.DefaultNewPhraseBeliefStrength), PhraseId = Guid.Empty, TimeStamp = DateTime.Now, UserId = currentUserId, Username = currentUsername }; return newPhraseBeliefDto; }
protected override PhraseBeliefDto InsertImpl(PhraseBeliefDto dto) { using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { PhraseBeliefData newPhraseBeliefData = EfHelper.AddToContext(dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); dto.Id = newPhraseBeliefData.Id; return dto; } }
private void CheckContraints(PhraseBeliefDto dto) { //REFERENTIAL INTEGRITY if (dto.PhraseId == Guid.Empty || !SeedData.Ton.ContainsPhraseId(dto.PhraseId)) throw new Exceptions.IdNotFoundException(dto.PhraseId); if (dto.UserId == Guid.Empty || !SeedData.Ton.ContainsUserId(dto.UserId)) throw new Exceptions.IdNotFoundException(dto.UserId); if (string.IsNullOrEmpty(dto.Username) || !(SeedData.Ton.GetUsername(dto.UserId) == dto.Username)) throw new ArgumentException("dto.Username"); }
protected override PhraseBeliefDto UpdateImpl(PhraseBeliefDto dto) { var results = from item in SeedData.Ton.PhraseBeliefs where item.Id == dto.Id select item; if (results.Count() == 1) { CheckContraints(dto); var phraseBeliefToUpdate = results.First(); SeedData.Ton.PhraseBeliefs.Remove(phraseBeliefToUpdate); dto.Id = Guid.NewGuid(); SeedData.Ton.PhraseBeliefs.Add(dto); UpdateReferences(phraseBeliefToUpdate, dto); return dto; } else { if (results.Count() == 0) throw new Exceptions.IdNotFoundException(dto.Id); else throw new Exceptions.VeryBadException(); } }
//protected override ICollection<PhraseBeliefDto> FetchImpl(ICollection<Guid> ids) //{ // var phraseBeliefDtos = new List<PhraseBeliefDto>(); // foreach (var id in ids) // { // phraseBeliefDtos.Add(FetchImpl(id)); // } // return phraseBeliefDtos; //} protected override PhraseBeliefDto UpdateImpl(PhraseBeliefDto dto) { var currentUserId = ((CustomIdentity)(Csla.ApplicationContext.User.Identity)).UserId; using (var ctx = LearnLanguagesContextManager.Instance.GetManager()) { var results = from phraseBeliefData in ctx.ObjectContext.PhraseBeliefDatas where phraseBeliefData.Id == dto.Id && phraseBeliefData.UserDataId == currentUserId select phraseBeliefData; if (results.Count() == 1) { var phraseBeliefData = results.First(); EfHelper.LoadDataFromDto(ref phraseBeliefData, dto, ctx.ObjectContext); ctx.ObjectContext.SaveChanges(); var updatedDto = EfHelper.ToDto(phraseBeliefData); return updatedDto; } else { if (results.Count() == 0) throw new Exceptions.IdNotFoundException(dto.Id); else { //results.count is not one or zero. either it's negative, which would be framework absurd, or its more than one, //which means that we have multiple phraseBeliefs with the same id. this is very bad. var errorMsg = string.Format(DalResources.ErrorMsgVeryBadException, DalResources.ErrorMsgVeryBadExceptionDetail_ResultCountNotOneOrZero); throw new Exceptions.VeryBadException(errorMsg); } } } }
/// <summary> /// Adds the PhraseBeliefDto to the context, loading UserData and PhraseDatas into the newly /// created PhraseBeliefData. Does NOT save changes to the context. /// </summary> public static PhraseBeliefData AddToContext(PhraseBeliefDto dto, LearnLanguagesContext context) { //CREATE THE NEW OBJECT var data = context.PhraseBeliefDatas.CreateObject(); //ASSIGN PROPERTIES data.PhraseDataId = dto.PhraseId; data.ReviewMethodId = dto.ReviewMethodId; data.Strength = dto.Strength; data.BelieverId = dto.BelieverId; data.Text = dto.Text; data.TimeStamp = dto.TimeStamp; data.UserDataId = dto.UserId; //ADD OBJECT TO CONTEXT context.PhraseBeliefDatas.AddObject(data); //RETURN OBJECT return data; }
protected override PhraseBeliefDto NewImpl(object criteria) { //to get to this point, we must have already been authenticated Debug.Assert(Csla.ApplicationContext.User.Identity.IsAuthenticated); if (criteria != null) throw new ArgumentException("criteria expected to be null"); var username = Csla.ApplicationContext.User.Identity.Name; var userId = (from u in SeedData.Ton.Users where u.Username == username select u.Id).FirstOrDefault(); if (userId == Guid.Empty) throw new Exceptions.UserNotAuthorizedException(); var dto = new PhraseBeliefDto() { Id = Guid.NewGuid(), Text = "", Strength = 1.0d, PhraseId = Guid.Empty, UserId = userId, Username = username }; return dto; }
public static PhraseBeliefDto ToDto(PhraseBeliefData data) { var dto = new PhraseBeliefDto() { Id = data.Id, BelieverId = data.BelieverId, PhraseId = data.PhraseDataId, ReviewMethodId = data.ReviewMethodId, Strength = data.Strength, Text = data.Text, TimeStamp = data.TimeStamp, UserId = data.UserDataId, Username = data.UserData.Username, }; return dto; }