Ejemplo n.º 1
0
        public async System.Threading.Tasks.Task Post([FromBody] Item item)
        {
            try
            {
                if (item == null)
                {
                    Response.StatusCode  = 403;
                    Response.ContentType = "application/json";
                    await Response.WriteAsync(JsonResponseFactory.CreateJson(null));

                    return;
                }

                item.User = userWork.GetEntities().Cast <User>().FirstOrDefault(u => u.UserName == User.Identity.Name);
                if (item.CurrencyId == null || item.CurrencyId == 0)
                {
                    item.CurrencyId = item.User.CurrencyId;
                }
                worker.AddEntity(item);
                Response.StatusCode  = 200;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(worker.GetEntities().LastOrDefault()));

                return;
            }
            catch
            {
                Response.StatusCode  = 400;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null));
            }
        }
Ejemplo n.º 2
0
        public async Task Post([FromBody] Family family)
        {
            try
            {
                var creator = UserWork.GetEntities().Cast <User>()
                              .FirstOrDefault(u => u.UserName == Response.HttpContext.User.Identity.Name);
                family.Users.Add(creator);
                family.CreatedAt = DateTime.Now;


                _worker.AddEntity(family);

                Response.StatusCode  = 200;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(
                                              _worker.GetEntities().LastOrDefault()));
            }
            catch (Exception ex)
            {
                Response.StatusCode  = ex is FamilyAlreadyExistException ? 403 : 400;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null,
                                                                         ex is FamilyAlreadyExistException ? new List <object> {
                    "Name"
                } : null,
                                                                         ex is FamilyAlreadyExistException ? new List <string> {
                    "family name is not unique"
                } : null));
            }
        }
Ejemplo n.º 3
0
        public async Task Post([FromBody] User user)
        {
            try
            {
                try
                {
                    user.Salt     = Salt.Create();
                    user.Password = Hash.Create(user.Password + _config.GetValue <string>("GlobalParameter"), user.Salt);
                    Worker.AddEntity(user);
                    Response.StatusCode  = 200;
                    Response.ContentType = "application/json";
                    await Response.WriteAsync(JsonResponseFactory.CreateJson(
                                                  Worker.GetEntities().Cast <User>().LastOrDefault()));

                    return;
                }
                catch (NullReferenceException)
                {
                    Response.ContentType = "application/json";
                    Response.StatusCode  = 400;
                    await Response.WriteAsync(JsonResponseFactory.CreateJson(null,
                                                                             new List <object> {
                        "Email | UserName"
                    }, new List <string> {
                        "email or username are not unique"
                    }));
                }
            }
            catch
            {
                Response.StatusCode  = 400;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null));
            }
        }
Ejemplo n.º 4
0
        public async Task Post([FromBody] Plan plan)
        {
            try
            {
                plan.UserId = userWorker.GetEntities().Cast <User>()
                              .FirstOrDefault(u => u.UserName == User.Identity.Name)
                              ?.Id;
                plan.Status     = "Active";
                plan.CurrencyId = (plan.CurrencyId == 0 || plan.CurrencyId == null)
                    ? ((User)userWorker.GetEntity(plan.UserId)).CurrencyId
                    : plan.CurrencyId;
                goalWorker.AddEntity(plan);
                Response.ContentType = "application/json";
                Response.StatusCode  = 200;

                await Response.WriteAsync(JsonResponseFactory.CreateJson(goalWorker.GetEntities().LastOrDefault()));
            }
            catch (Exception ex)
            {
                Response.ContentType = "application/json";
                Response.StatusCode  = 400;
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null));
            }
        }
Ejemplo n.º 5
0
        public async Task AddInvite([FromHeader] string userName)
        {
            try
            {
                var user        = UserWork.GetEntities().Cast <User>().FirstOrDefault(u => u.UserName == userName);
                var currentUser = UserWork.GetEntities().Cast <User>()
                                  .FirstOrDefault(u => u.UserName == User.Identity.Name);
                if (user != null)
                {
                    if (currentUser.Family != null)
                    {
                        if (currentUser?.Family.Users.FirstOrDefault()?.UserName == User.Identity.Name)
                        {
                            InviteWorker.AddEntity(new Invite
                            {
                                Date = DateTime.Now, FamilyId = (int)currentUser.FamilyId, UserName = userName
                            });
                            Response.StatusCode  = 200;
                            Response.ContentType = "application/json";
                            await Response.WriteAsync(
                                JsonResponseFactory.CreateJson(InviteWorker.GetEntities().LastOrDefault()));

                            return;
                        }

                        Response.StatusCode  = 403;
                        Response.ContentType = "application/json";
                        await Response.WriteAsync(JsonResponseFactory.CreateJson(null,
                                                                                 new List <object> {
                            ""
                        }, new List <string> {
                            "Current user is not owner of current family"
                        }));

                        return;
                    }

                    Response.StatusCode  = 403;
                    Response.ContentType = "application/json";
                    await Response.WriteAsync(JsonResponseFactory.CreateJson(null, new List <object> {
                        ""
                    },
                                                                             new List <string> {
                        "Current user is not in family now"
                    }));

                    return;
                }

                Response.StatusCode  = 404;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null, new List <object> {
                    "username"
                },
                                                                         new List <string> {
                    "User with current username not found"
                }));
            }
            catch
            {
                Response.StatusCode  = 400;
                Response.ContentType = "application/json";
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null));
            }
        }
        public async Task Post([FromBody] Transaction transaction)
        {
            try
            {
                if (transaction.ItemId != null)
                {
                    ((ItemWorker)itemWork).ManageItemData(itemWork.GetEntity(transaction.ItemId), transaction.Money, true);
                }

                if ((transaction.ItemId != null && transaction.WorkType != null) ||
                    (transaction.ItemId == null && transaction.WorkType == null))
                {
                    Response.StatusCode  = 400;
                    Response.ContentType = "application/json";
                    await Response.WriteAsync(JsonResponseFactory.CreateJson(null,
                                                                             new List <object> {
                        "WorkType | ItemId"
                    },
                                                                             new List <string>()
                    {
                        "The transaction can be only outgo or income.\n If outgo the ItemId field must be set but itemId=null , else Item id must be set and WorkType=null"
                    }));

                    return;
                }
                transaction.User = userWork.GetEntities().Cast <User>()
                                   .FirstOrDefault(u => u.UserName == User.Identity.Name);
                transaction.CurrencyId = (transaction.CurrencyId == 0 || transaction.CurrencyId == null)?transaction.User.CurrencyId:transaction
                                         .CurrencyId;
                if (transaction.User.CurrencyId != transaction.CurrencyId)
                {
                    var userCurrency    = userWork.GetEntities().Cast <User>().First(u => u.UserName == User.Identity.Name).Currency;
                    var currentCurrency = (Currency)currencyWork.GetEntity(transaction.CurrencyId);
                    transaction.Money      = transaction.Money * currentCurrency.rate / userCurrency.rate;
                    transaction.CurrencyId = userCurrency.Id;
                }
                if (transaction.ItemId != null)
                {
                    transaction.User.Money -= Math.Abs(transaction.Money);
                }
                else
                {
                    transaction.User.Money += Math.Abs(transaction.Money);
                }

                transactionWork.AddEntity(transaction);

                Response.ContentType = "application/json";

                Response.StatusCode = 200;
                await Response.WriteAsync(JsonResponseFactory.CreateJson(
                                              transactionWork.GetEntities().Cast <Transaction>().LastOrDefault()));
            }
            catch
            {
                Response.ContentType = "application/json";

                Response.StatusCode = 400;
                await Response.WriteAsync(JsonResponseFactory.CreateJson(null));
            }
        }