//Данный метод искуственно сделан через двойной context.SaveChanges, что бы выполнить задание - 2 изменения в одной транзацкии.
        public Boolean CreateFamily(Int32 personId, String familyName, Int32 budget)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto personDto = context.CPeopleDto.Where(p => p.PersonID == personId).FirstOrDefault();
                    if (personDto == null)
                    {
                        log.Warn("Can't create new family because person (id = {0}) doesn't exist", personId);
                        return(false);
                    }
                    if (personDto.FamilyID != null)
                    {
                        log.Warn("Can't create new family because person (id = {0}) already has family!", personId);
                        return(false);
                    }

                    using (System.Data.Entity.DbContextTransaction transaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            CFamilyDto familyDto = new CFamilyDto();
                            familyDto.FamilyName     = familyName;
                            familyDto.Budget         = budget;
                            familyDto.MembersCounter = 1;

                            context.CFamiliesDto.Add(familyDto);
                            context.SaveChanges();

                            Int32 familyId = familyDto.FamilyID;
                            personDto.FamilyID = familyId;
                            context.SaveChanges();

                            transaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            log.Error(ex, "There is an error in transaction when created new family. Message: {0}", ex.Message);
                            transaction.Rollback();
                            return(false);
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to create new family in DB. Message: {0}", ex.Message);
                return(false);
            }
        }
Beispiel #2
0
        public bool DeleteItemByKey(CReminderKey key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CReminderDto reminder = context.CRemindersDto.Where(r => r.PersonID == key.PersonId && r.Title.Equals(key.Title)).FirstOrDefault();
                    if (reminder == null)
                    {
                        log.Info("Can't delete reminder because it doesn't exist in database id = {0}, Title: {1}", key.PersonId, key.Title);
                        return(false);
                    }

                    context.CRemindersDto.Remove(reminder);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete CReminders (id = {1} title: {2}). Message: {0}", ex.Message, key.PersonId, key.Title);
                return(false);
            }

            return(true);
        }
        public bool DeleteItemByKey(int key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto person = context.CPeopleDto.Where(p => p.PersonID == key).FirstOrDefault();
                    if (person == null)
                    {
                        log.Info("Can't delete person data because it doesn't exist in database (personId = {0})", key);
                        return(false);
                    }

                    context.CPeopleDto.Remove(person);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete person data from DB (PersonId = {1}). Message: {0}", ex.Message, key);
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        public bool DeleteItemByKey(Int32 key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPaymentDto payment = context.CPaymentsDto.Where(p => p.PaymentID == key).FirstOrDefault();
                    if (payment == null)
                    {
                        log.Info("Can't update payment because it doesn't exist in database (paymentId = {0})", key);
                        return(false);
                    }

                    context.CPaymentsDto.Remove(payment);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete payment (paymentId = {1}). Message: {0}", ex.Message, key);
                return(false);
            }

            return(true);
        }
        public bool DeleteItemByKey(CSpendingHistoryKey key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSpendingHistoryDto spendingHistory = context.CSpendingHistoriesDto.Where(h => h.PersonID == key.PersonId &&
                                                                                              h.CategoryID == key.CategoryId &&
                                                                                              h.Month == key.Month)
                                                          .FirstOrDefault();
                    if (spendingHistory == null)
                    {
                        log.Info("Can't Delete SpendingHistory because it doesn't exist in database (personId = {0}, categoryId = {1}, Month: {2})",
                                 key.PersonId, key.CategoryId, key.Month);
                        return(false);
                    }

                    context.CSpendingHistoriesDto.Remove(spendingHistory);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete SpendingHistory (personId = {1}, categoryId = {2}, Month: {3}). Message: {0}",
                          ex.Message, key.PersonId, key.CategoryId, key.Month);
                return(false);
            }

            return(true);
        }
        public bool UpdateItem(CPersonDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto person = context.CPeopleDto.Where(p => p.PersonID == item.PersonID).FirstOrDefault();
                    if (person == null)
                    {
                        log.Info("Can't update person data because it doesn't exist in database (personId = {0})", item.PersonID);
                        return(false);
                    }

                    person.Name     = item.Name;
                    person.FamilyID = item.FamilyID;
                    person.Budget   = item.Budget;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update person in DB (PersonId = {1}). Message: {0}", ex.Message, item.PersonID);
                return(false);
            }

            return(true);
        }
        public bool UpdateItem(CSpendingHistoryDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSpendingHistoryDto spendingHistory = context.CSpendingHistoriesDto.Where(h => h.PersonID == item.PersonID &&
                                                                                              h.CategoryID == item.CategoryID &&
                                                                                              h.Month == item.Month)
                                                          .FirstOrDefault();
                    if (spendingHistory == null)
                    {
                        log.Info("Can't update SpendingHistory because it doesn't exist in database (personId = {0}, categoryId = {1}, Month: {2})",
                                 item.PersonID, item.CategoryID, item.Month);
                        return(false);
                    }

                    spendingHistory.Spended = item.Spended;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update SpendingHistory (personId = {1}, categoryId = {2}, Month: {3}). Message: {0}",
                          ex.Message, item.PersonID, item.CategoryID, item.Month);
                return(false);
            }

            return(true);
        }
Beispiel #8
0
        public Boolean DeleteItemByKey(Int32 key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSubCategoryDto subCategory = context.CSubCategoriesDto.Where(s => s.SubCategoryID == key).FirstOrDefault();
                    if (subCategory == null)
                    {
                        log.Info("Can't delete subCategory because it doesn't exist in database (SubCategoryid = {0})", key);
                        return(false);
                    }

                    context.CSubCategoriesDto.Remove(subCategory);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete subCategory (SubCategoryId = {1}). Message: {0}", ex.Message, key);
                return(false);
            }

            return(true);
        }
Beispiel #9
0
        public Boolean UpdateItem(CSubCategoryDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSubCategoryDto subCategory = context.CSubCategoriesDto.Where(s => s.SubCategoryID == item.SubCategoryID).FirstOrDefault();
                    if (subCategory == null)
                    {
                        log.Info("Can't update subCategory because it doesn't exist in database (SubCategoryid = {0})", item.SubCategoryID);
                        return(false);
                    }

                    subCategory.ParentCategoryID = item.ParentCategoryID;
                    subCategory.Title            = item.Title;
                    subCategory.Description      = item.Description;

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update subCategory (SubCategoryid = {1}). Message: {0}", ex.Message, item.SubCategoryID);
                return(false);
            }

            return(true);
        }
Beispiel #10
0
        public bool UpdateItem(CReminderDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CReminderDto reminder = context.CRemindersDto.Where(r => r.PersonID == item.PersonID && r.Title.Equals(item.Title)).FirstOrDefault();

                    if (reminder == null)
                    {
                        log.Info("Can't update reminder because it doesn't exist in database id = {0}, Title: {1}", item.PersonID, item.Title);
                        return(false);
                    }

                    reminder.DayOfMonth  = item.DayOfMonth;
                    reminder.Description = item.Description;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update CReminders (id = {1} title: {2}). Message: {0}", ex.Message, item.PersonID, item.Title);
                return(false);
            }

            return(true);
        }
        public Boolean JoinFamily(Int32 personId, Int32 familyId)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CFamilyDto familyDto = context.CFamiliesDto.Where(f => f.FamilyID == familyId).FirstOrDefault();
                    if (familyDto == null)
                    {
                        log.Warn("Person (id = {0}) cant join to the family (id = {1}) because family doesn't exist", personId, familyId);
                        return(false);
                    }

                    CPersonDto personDto = context.CPeopleDto.Where(p => p.PersonID == personId).FirstOrDefault();
                    if (personDto == null)
                    {
                        log.Warn("Person (id = {0}) cant join to the family (id = {1}) because person doesn't exist", personId, familyId);
                        return(false);
                    }

                    personDto.Family = familyDto;
                    familyDto.MembersCounter++;
                    context.SaveChanges();
                }

                return(true);
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying person (id={1}) to join to family (id={2}) in DB. Message: {0}", ex.Message, personId, familyId);
                return(false);
            }
        }
        public bool UpdateItem(CLoginDataDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CLoginDataDto loginData = context.CLoginDatasDto.Where(l => l.PersonID == item.PersonID).FirstOrDefault();
                    if (loginData == null)
                    {
                        log.Info("Can't update loginData because it doesn't exist in database (personId = {0})", item.PersonID);
                        return(false);
                    }

                    loginData.email        = item.email;
                    loginData.Username     = item.Username;
                    loginData.PasswordHash = item.PasswordHash;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update loginData (PersonId = {1}). Message: {0}", ex.Message, item.PersonID);
                return(false);
            }

            return(true);
        }
        public bool DeleteItemByKey(CPersonalTargetKey key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonalTargetDto target = context.CPersonalTargetsDto.Where(t => t.PersonID == key.PersonId &&
                                                                                  t.CategoryID == key.CategoryId &&
                                                                                  t.Month == key.Month)
                                                .FirstOrDefault();
                    if (target == null)
                    {
                        log.Info("Can't delete CPersonalTarget because it doesn't exist in database (PersonId = {0}, categoryId = {1}, date = {2})",
                                 key.PersonId, key.CategoryId, key.Month);
                        return(false);
                    }

                    context.CPersonalTargetsDto.Remove(target);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete CPersonalTargets (PersonId = {1}, categoryId = {2}, date = {3}). Message: {0}",
                          ex.Message, key.PersonId, key.CategoryId, key.Month);
                return(false);
            }

            return(true);
        }
Beispiel #14
0
        public bool UpdateItem(CPaymentDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPaymentDto payment = context.CPaymentsDto.Where(p => p.PaymentID == item.PaymentID).FirstOrDefault();
                    if (payment == null)
                    {
                        log.Info("Can't update payment because it doesn't exist in database (paymentId = {0})", item.PaymentID);
                        return(false);
                    }

                    payment.SubCategoryID = item.SubCategoryID;
                    payment.Date          = item.Date;
                    payment.Spended       = item.Spended;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update payment (paymentId = {1}). Message: {0}", ex.Message, item.PaymentID);
                return(false);
            }

            return(true);
        }
        public bool UpdateItem(CPersonalTargetDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonalTargetDto target = context.CPersonalTargetsDto.Where(t => t.PersonID == item.PersonID &&
                                                                                  t.CategoryID == item.CategoryID &&
                                                                                  t.Month == item.Month)
                                                .FirstOrDefault();
                    if (target == null)
                    {
                        log.Info("Can't update CPersonalTarget because it doesn't exist in database (PersonId = {0}, categoryId = {1}, date = {2})",
                                 item.PersonID, item.CategoryID, item.Month);
                        return(false);
                    }

                    target.Type   = item.Type;
                    target.Amount = item.Amount;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update CPersonalTarget (PersonId = {1}, categoryId = {2}, date = {3}). Message: {0}",
                          ex.Message, item.PersonID, item.CategoryID, item.Month);
                return(false);
            }

            return(true);
        }
        public bool DeleteItemByKey(int key)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CFamilyDto family = context.CFamiliesDto.Where(f => f.FamilyID == key).FirstOrDefault();
                    if (family == null)
                    {
                        log.Info("Can't delete family because it doesn't exist in database (familyId = {0})", key);
                        return(false);
                    }

                    context.CFamiliesDto.Remove(family);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to delete family (FamilyId = {1}). Message: {0}", ex.Message, key);
                return(false);
            }

            return(true);
        }
        public bool UpdateItem(CFamilyDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CFamilyDto family = context.CFamiliesDto.Where(f => f.FamilyID == item.FamilyID).FirstOrDefault();
                    if (family == null)
                    {
                        log.Info("Can't update family because it doesn't exist in database (familyId = {0})", item.FamilyID);
                        return(false);
                    }

                    family.FamilyName = item.FamilyName;
                    family.Budget     = item.Budget;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to update family in BD (FamilyId = {1}). Message: {0}", ex.Message, item.FamilyID);
                return(false);
            }

            return(true);
        }
Beispiel #18
0
        public Boolean AddItem(CSubCategoryDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    context.CSubCategoriesDto.Add(item);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add subCategory into DB. Message: {0}", ex.Message);
                return(false);
            }

            return(true);
        }
Beispiel #19
0
        public bool AddItem(CPaymentDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    context.CPaymentsDto.Add(item);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add new payment into DB. Message: {0}", ex.Message);
                return(false);
            }

            return(true);
        }
Beispiel #20
0
        public Boolean TyrEditSubCategory(Int32 personId, Int32 subCategoryId, String newCategoryTitle, String newSubCategoryTitle, String newSubCategoryDescription)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CSubCategoryDto subCategoryDto = context.CSubCategoriesDto.Where(s => s.SubCategoryID == subCategoryId).FirstOrDefault();
                    if (subCategoryDto == null)
                    {
                        log.Warn("Can't find subCategory (id = {0}) to edit)", subCategoryId);
                        return(false);
                    }

                    if (subCategoryDto.PersonID != personId)
                    {
                        log.Warn("Can't edit subCategory because person (id = {0}) not an owner of subCategory (id = {1})", personId, subCategoryId);
                        return(false);
                    }

                    if (newCategoryTitle != subCategoryDto.Category.Title)
                    {
                        CCategoryDto newCategoryDto = context.CCategoriesDto.Where(c => c.Title == newCategoryTitle).FirstOrDefault();
                        if (newCategoryDto == null)
                        {
                            log.Warn("Can't find new category (title = {0}) to edit subCategory(id = {1}))", newCategoryTitle, subCategoryId);
                            return(false);
                        }

                        subCategoryDto.ParentCategoryID = newCategoryDto.CategoryID;
                    }

                    subCategoryDto.Title       = newSubCategoryTitle;
                    subCategoryDto.Description = newSubCategoryDescription;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to edit subCategory (id = {1}) into DB. Message: {0}", ex.Message, subCategoryId);
                return(false);
            }

            return(true);
        }
Beispiel #21
0
        public Int32 AddPayment(Int32 personId, DateTime date, String categoryTitle, String subCategoryTitle, Decimal spended)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto personDto = context.CPeopleDto
                                           .Include("SubCategories.Category")
                                           .Include("SubCategories.Payments")
                                           .Where(p => p.PersonID == personId)
                                           .FirstOrDefault();
                    if (personDto == null)
                    {
                        log.Info("Can't add payment because person (id = {0}) doesn't exist", personId);
                        return(0);
                    }

                    CSubCategoryDto subCategoryDto = personDto.SubCategories.Where(s => s.Title.Equals(subCategoryTitle) && s.Category.Title.Equals(categoryTitle)).FirstOrDefault();
                    if (subCategoryDto == null)
                    {
                        log.Info("Can't add payment because person (id = {0}) doesn't have subCategory (title = {1}) with category (title = {2})",
                                 personId, subCategoryTitle, categoryTitle);
                        return(0);
                    }

                    CPaymentDto paymentDto = new CPaymentDto();
                    paymentDto.Date          = date;
                    paymentDto.SubCategoryID = subCategoryDto.SubCategoryID;
                    paymentDto.Spended       = spended;

                    //AddItem(paymentDto);
                    subCategoryDto.Payments.Add(paymentDto);
                    context.SaveChanges();

                    return(paymentDto.PaymentID);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add new payment into DB. Message: {0}", ex.Message);
                return(0);
            }
        }
Beispiel #22
0
        public Int32 TryAddSubCategory(Int32 personId, String categoryTitle, String subCategoryTitle, String subCategoryDescription)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    CPersonDto personDto = context.CPeopleDto
                                           .Include("SubCategories.Category")
                                           .Where(p => p.PersonID == personId)
                                           .FirstOrDefault();
                    if (personDto == null)
                    {
                        log.Info("Can't add payment because person (id = {0}) doesn't exist", personId);
                        return(0);
                    }


                    CCategoryDto category = context.CCategoriesDto.Where(c => c.Title.Equals(categoryTitle)).FirstOrDefault();
                    if (category == null)
                    {
                        log.Info("Can't add SubCategory because category (title  = {0}) doesn't exist", categoryTitle);
                        return(0);
                    }

                    CSubCategoryDto subCatDto = new CSubCategoryDto();
                    subCatDto.Person      = personDto;
                    subCatDto.Title       = subCategoryTitle;
                    subCatDto.Description = subCategoryDescription;
                    subCatDto.Category    = category;

                    context.CSubCategoriesDto.Add(subCatDto);
                    context.SaveChanges();

                    return(subCatDto.SubCategoryID);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add subCategory into DB. Message: {0}", ex.Message);
                return(0);
            }
        }