Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromForm] Member model)
        {
            var status = new StatusModel <Member>
            {
                Model  = model,
                Errors = new System.Collections.Generic.List <string>()
            };

            // data validation to be done here
            if (string.IsNullOrEmpty(model.Name))
            {
                status.Errors.Add("Name of member is not specified.");
            }
            if (!string.IsNullOrEmpty(model.PhoneNumber) && model.PhoneNumber.Length == 10 && !long.TryParse(model.PhoneNumber, NumberStyles.Integer, null, out _))
            {
                status.Errors.Add("Specified phone number is not number");
            }

            if (status.Errors.Count != 0)
            {
                return(View("Status", status));
            }

            model.Id            = Guid.NewGuid().ToString();
            model.OwnedUnits    = null;
            model.Account       = null;
            model.LastUpdatedOn = DateTime.Now;

            model.Account = new MemberAccount {
                MemberId = model.Id, Id = Guid.NewGuid().ToString()
            };
            _dbContext.Members.Add(model);
            try
            {
                if (await _dbContext.SaveChangesAsync() <= 0)
                {
                    status.Errors.Add("Failed to update Database. There may be connection error. Contact administrator now.");
                }

                status.ModelId = model.Id;
            }
            catch (System.Exception e)
            {
                status.Errors.Add(e.Message);
            }

            return(View("Status", status));
        }
        public async Task <IActionResult> Create([FromForm] TransactionPassthrough model)
        {
            var status = new StatusModel <TransactionPassthrough>
            {
                Model  = model,
                Errors = new System.Collections.Generic.List <string>()
            };

            // data validation to be done here
            if (string.IsNullOrEmpty(model.Name))
            {
                status.Errors.Add("Name of passthough is not specified.");
            }



            if (status.Errors.Count != 0)
            {
                return(View("Status", status));
            }

            model.Id = Guid.NewGuid().ToString();
            _dbContext.TransactionPassthrough.Add(model);
            try
            {
                if (await _dbContext.SaveChangesAsync() <= 0)
                {
                    status.Errors.Add("Failed to update Database. There may be connection error. Contact administrator now.");
                }

                status.ModelId = model.Id;
            }
            catch (System.Exception e)
            {
                status.Errors.Add(e.Message);
            }

            return(View("Status", status));
        }
        public async Task <IActionResult> Create([FromForm] Unit model)
        {
            var status = new StatusModel <Unit>
            {
                Model  = model,
                Errors = new System.Collections.Generic.List <string>()
            };

            // data validation to be done here
            if (string.IsNullOrEmpty(model.Name))
            {
                status.Errors.Add("Name of unit is not specified.");
            }
            if (!model.Area.HasValue)
            {
                status.Errors.Add("Area of unit is not specified.");
            }
            if (string.IsNullOrWhiteSpace(model.OwnerId))
            {
                status.Errors.Add("Owner of unit is not specified.");
            }
            else
            {
                var owner = await _dbContext.Members.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.OwnerId);

                if (owner == null)
                {
                    var expandedId = smallIdService.GetFullId <Member>(model.OwnerId);
                    if (expandedId == null ||
                        await _dbContext.Members.AsNoTracking().FirstOrDefaultAsync(m => m.Id == expandedId) == null)
                    {
                        status.Errors.Add("Owner of unit is not valid.");
                    }
                    else
                    {
                        model.OwnerId = expandedId;
                    }
                }
            }
            model.Owner = null;

            if (status.Errors.Count != 0)
            {
                return(View("Status", status));
            }

            model.Id = Guid.NewGuid().ToString();
            _dbContext.Units.Add(model);
            try
            {
                if (await _dbContext.SaveChangesAsync() <= 0)
                {
                    status.Errors.Add("Failed to update Database. There may be connection error. Contact administrator now.");
                }

                status.ModelId = model.Id;
                model.Owner    = await _dbContext.Members.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.OwnerId);
            }
            catch (System.Exception e)
            {
                status.Errors.Add(e.Message);
            }

            return(View("Status", status));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([FromForm] TransactionCreationModel creationModel)
        {
            var model  = creationModel.Model;
            var status = new StatusModel <Transaction>
            {
                Model  = model,
                Errors = new System.Collections.Generic.List <string>()
            };

            // data validation to be done here
            if (string.IsNullOrEmpty(model.Description))
            {
                status.Errors.Add("Description of transaction is not specified.");
            }
            if (!model.Date.HasValue)
            {
                status.Errors.Add("Date of transaction is not specified.");
            }
            if (model.Amount == 0d)
            {
                status.Errors.Add("Amount of transaction is not valid.");
            }

            if (string.IsNullOrWhiteSpace(model.LedgerId))
            {
                status.Errors.Add("Ledger of transaction is not specified.");
            }
            else
            {
                var ledger = await _dbContext.Ledgers.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.LedgerId);

                if (ledger == null)
                {
                    status.Errors.Add("Ledger of transaction is not valid.");
                }
            }
            if (string.IsNullOrWhiteSpace(model.PassthroughId))
            {
                status.Errors.Add("Passthrough of transaction is not specified.");
            }
            else
            {
                var ledger = await _dbContext.TransactionPassthrough.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.PassthroughId);

                if (ledger == null)
                {
                    status.Errors.Add("Passthrough of transaction is not valid.");
                }
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                var ownerAccount = await _dbContext.MemberAccounts.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.AccountId);

                if (ownerAccount == null)
                {
                    status.Errors.Add("Owner of transaction is not valid.");
                }
            }
            if (!string.IsNullOrWhiteSpace(model.UnitId))
            {
                var unit = await _dbContext.Units.AsNoTracking().FirstOrDefaultAsync(m => m.Id == model.UnitId);

                if (unit == null)
                {
                    status.Errors.Add("Unit of transaction is not valid.");
                }
            }

            model.Account     = null;
            model.Passthrough = null;
            model.Unit        = null;
            model.Ledger      = null;
            model.Passthrough = null;

            return(Json(status));

            if (status.Errors.Count != 0)
            {
                return(View("Status", status));
            }

            model.Id = Guid.NewGuid().ToString();
            _dbContext.Transactions.Add(model);
            try
            {
                if (await _dbContext.SaveChangesAsync() <= 0)
                {
                    status.Errors.Add("Failed to update Database. There may be connection error. Contact administrator now.");
                }

                status.ModelId = model.Id;
            }
            catch (System.Exception e)
            {
                status.Errors.Add(e.Message);
            }

            return(View("Status", status));
        }