Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
            }
        }
Пример #4
0
        //Данный метод искуственно сделан через двойной 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);
            }
        }
Пример #5
0
        public bool AddItem(CFamilyDto item)
        {
            try
            {
                using (SpendingHelperDBEntities context = new SpendingHelperDBEntities())
                {
                    context.CFamiliesDto.Add(item);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex, "Some error occure while trying to add family into DB. Message: {0}", ex.Message);
                return(false);
            }

            return(true);
        }