예제 #1
0
        public BaseResponse AddInviteKey(InviteKeyRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        var family = db.Families.Where(x => x.Id == request.FamilyId).FirstOrDefault();

                        var inviteKey = new InviteKey
                        {
                            Key = request.Key,
                            Family = family
                        };
                        db.InviteKeys.Add(inviteKey);
                        db.SaveChanges();
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #2
0
        public BaseResponse AddReport(ReportRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        var family = db.Families.Where(x => x.Id == request.FamilyId).FirstOrDefault();
                        var person = db.Persons.Where(x => x.FamilyId == request.FamilyId && x.Id == request.PersonId).FirstOrDefault();

                        var model = new Report
                        {
                            Name = request.Name,
                            Text = request.Text,
                            Date = request.Date,
                            Family = family,
                            Person = person
                        };

                        db.Reports.Add(model);
                        db.SaveChanges();
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Shared.Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #3
0
        public BaseResponse RegistrationNewWithKey(RegistrationRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (request == null)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Помилка, пусті дані";
                        }
                        else if (request.Key != null)
                        {
                            if (!db.InviteKeys.Any(x => x.Key == request.Key))
                            {
                                response.BaseIsSuccess = false;
                                response.BaseMessage = "Помилка, Невірний ключ";
                            }
                            else
                            {
                                var familyId = db.InviteKeys.Where(x => x.Key == request.Key).FirstOrDefault().FamilyId;
                                var key = db.InviteKeys.Where(x => x.Key == request.Key).FirstOrDefault();
                                db.InviteKeys.Remove(key);

                                var family = db.Families.Where(x => x.Id == familyId).FirstOrDefault();

                                var person = new Person
                                {
                                    Name = request.PersonName,
                                    Role = "U",
                                    Family = family,
                                    Login = request.Login,
                                    Password = request.Password
                                };

                                db.Persons.Add(person);
                                db.SaveChanges();
                            }
                        }
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #4
0
        public BaseResponse UpdatePurpose(UpdatePurposeRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        var purpose = db.Purposes.Where(x => x.Id == request.PurposeId).FirstOrDefault();
                        var purse = db.Purses.Where(x => x.Id == request.PurseId).FirstOrDefault();
                        if (purse.Size <= request.NewSize)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Недостатньо коштів";
                        }
                        if (purpose.CurrentSize + request.NewSize > purpose.FinalSize)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Перевищення ліміту";
                        }
                        if (purpose != null && purse != null)
                        {
                            purse.Size -= request.NewSize;
                            purpose.CurrentSize += request.NewSize;

                            db.Purses.Update(purse);
                            db.Purposes.Update(purpose);
                            db.SaveChanges();
                        }
                        else
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Помилка додавання";
                        }
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Shared.Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #5
0
        public BaseResponse AddPurse(PurseRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (request == null)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Не можна додати порожній гаманець";
                        }
                        else if (db.Purses.Any(x => x.Name == request.Name))
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Гаманець з такою назвою вже існує";
                        }
                        else
                        {
                            var currency = db.Currencies.Where(x => x.Id == request.CurrencyId).FirstOrDefault();
                            var model = new Purse
                            {
                                Name = request.Name,
                                Size = request.Size,
                                Currency = currency,
                                FamilyId = request.FamilyId,
                                PersonId = request.PersonId
                            };

                            db.Purses.Add(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = ex.Message;
                }

                return response;
            }));
        }
예제 #6
0
        public BaseResponse AddCategory(CategoryRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (request.Category == default)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Не можна додати порожню категорію";
                        }
                        else if (db.Categories.Any(x => x.Name == request.Category.Name && x.FamilyId == request.Category.FamilyId))
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Така категорія вже існує";
                        }
                        else
                        {
                            var family = db.Families.Where(x => x.Id == request.Category.FamilyId).FirstOrDefault();

                            var model = new Category
                            {
                                Name = request.Category.Name,
                                Family = family
                            };
                            db.Categories.Add(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Shared.Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #7
0
        public BaseResponse AddPurpose(PurposeRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (db.Purposes.Any(x => x.Name == request.Name && x.FamilyId == request.FamilyId))
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Така ціль заощадження вже є";
                        }
                        else
                        {
                            var currency = db.Currencies.Where(x => x.Id == request.CurrencyId).FirstOrDefault();

                            var model = new Purpose
                            {
                                Name = request.Name,
                                FinalSize = request.FinalSize,
                                CurrentSize = 0,
                                Currency = currency,
                                FamilyId = request.FamilyId,
                                PersonId = request.PersonId
                            };

                            db.Purposes.Add(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = ex.InnerException.Message;
                }

                return response;
            }));
        }
예제 #8
0
        public BaseResponse DeleteReport(DeleteReportRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        var report = db.Reports.Where(x => x.Id == request.ReportId && x.FamilyId == request.FamilyId).FirstOrDefault();

                        db.Reports.Remove(report);
                        db.SaveChanges();
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Shared.Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #9
0
        public BaseResponse DeleteCategory(CategoryRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        var category = db.Categories.Where(x => x.Id == request.Category.Id).FirstOrDefault();

                        db.Categories.Remove(category);
                        db.SaveChanges();
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Shared.Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #10
0
        public BaseResponse RegistrationNew(RegistrationRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (request == null)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = "Помилка, пусті дані";
                        }
                        else
                        {
                            var family = new Family
                            {
                                Name = request.FamilyName
                            };

                            db.Families.Add(family);
                            db.SaveChanges();

                            var familyFormContext = db.Families.Where(x => x.Name == family.Name).FirstOrDefault();

                            var uah = db.Currencies.Where(x => x.Id == 1).FirstOrDefault();
                            var usd = db.Currencies.Where(x => x.Id == 2).FirstOrDefault();
                            var eur = db.Currencies.Where(x => x.Id == 3).FirstOrDefault();
                            var plz = db.Currencies.Where(x => x.Id == 4).FirstOrDefault();

                            db.Purses.Add(new Purse {
                                Name = "Гаманець з гривнями", Size = 0, Currency = uah, Family = familyFormContext, PersonId = null
                            });
                            db.Purses.Add(new Purse {
                                Name = "Гаманець з доларами", Size = 0, Currency = usd, Family = familyFormContext, PersonId = null
                            });
                            db.Purses.Add(new Purse {
                                Name = "Гаманець з євро", Size = 0, Currency = eur, Family = familyFormContext, PersonId = null
                            });
                            db.Purses.Add(new Purse {
                                Name = "Гаманець з злотими", Size = 0, Currency = plz, Family = familyFormContext, PersonId = null
                            });

                            db.Categories.Add(new Category {
                                Name = "Розваги", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Житло", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Комунальні послуги", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Навчання", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Їжа", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Транспорт", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Робота", Family = familyFormContext
                            });
                            db.Categories.Add(new Category {
                                Name = "Інше", Family = familyFormContext
                            });

                            var person = new Person
                            {
                                Name = request.PersonName,
                                Role = "H",
                                Family = familyFormContext,
                                Login = request.Login,
                                Password = request.Password
                            };

                            db.Persons.Add(person);
                            db.SaveChanges();
                        }
                    }
                }
                catch
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = Constants.BAD_REQUEST;
                }

                return response;
            }));
        }
예제 #11
0
        public BaseResponse AddIncomeOrExpense(ChangeMoneyRequest request)
        {
            return(GetResponse(() => {
                var response = new BaseResponse();
                try
                {
                    using (FamilyFinanceContext db = new FamilyFinanceContext())
                    {
                        if (request.FamilyId == null && request.PersonId == null)
                        {
                            response.BaseIsSuccess = false;
                            response.BaseMessage = Shared.Constants.NEED_AUTHORIZE;
                        }
                        else
                        {
                            var category = db.Categories.Where(x => x.Name == request.Category.Name && x.FamilyId == request.FamilyId).FirstOrDefault();
                            var currency = db.Currencies.Where(x => x.Name == request.Currency.Name).FirstOrDefault();

                            var purse = db.Purses.Where(x => x.Name == request.Purse.Name && x.FamilyId == request.FamilyId).FirstOrDefault();
                            if (request.Type == "I")
                            {
                                purse.Size += request.Size;
                            }
                            else
                            {
                                if (request.Size <= purse.Size)
                                {
                                    purse.Size -= request.Size;
                                }
                                else
                                {
                                    response.BaseIsSuccess = false;
                                    response.BaseMessage = "Недостатньо коштів для операції";
                                }
                            }
                            db.Purses.Update(purse);

                            var model = new ChangeMoney
                            {
                                Name = request.Name,
                                Size = request.Size,
                                Date = request.Date,
                                Type = request.Type,
                                Category = category,
                                Currency = currency,
                                FamilyId = request.FamilyId,
                                PersonId = request.PersonId
                            };

                            db.ChangeMoneys.Add(model);
                            db.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.BaseIsSuccess = false;
                    response.BaseMessage = ex.Message + "; Inner: " + ex.InnerException.Message;
                }

                return response;
            }));
        }