public IActionResult Create([Bind("FirstName,LastName,Username,Password,Employee,StoreName")] PersonViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var person = new Domain.Model.Person
                    {
                        Id          = viewModel.PersonId,
                        FirstName   = viewModel.FirstName,
                        LastName    = viewModel.LastName,
                        Username    = viewModel.Username,
                        Password    = viewModel.Password,
                        EmployeeTag = false,
                        StoreId     = RepoStore.GetStoresByName(viewModel.StoreName).First(p => p.Name.ToLower() == viewModel.StoreName.ToLower()).Id,
                    };
                    List <string> myStores = new List <string> ();
                    foreach (var val in RepoStore.GetStoresByName().ToList())
                    {
                        myStores.Add(val.Name);
                    }
                    ViewData["StoreName"] = new SelectList(myStores);

                    RepoPers.AddPerson(person);
                    RepoPers.Save();

                    return(RedirectToAction(nameof(Index)));
                }
                return(View(viewModel));
            }
            catch
            {
                return(View(viewModel));
            }
        }
        public IActionResult Edit([FromRoute] int id, [Bind("FirstName,LastName,Username,Password,Employee,StoreName")] PersonViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Domain.Model.Person person = RepoPers.GetPersonById(id);
                    person.FirstName   = viewModel.FirstName;
                    person.LastName    = viewModel.LastName;
                    person.Username    = viewModel.Username;
                    person.Password    = viewModel.Password;
                    person.EmployeeTag = viewModel.Employee;
                    person.StoreId     = RepoStore.GetStoresByName(viewModel.StoreName).First(p => p.Name.ToLower() == viewModel.StoreName.ToLower()).Id;
                    RepoPers.UpdatePerson(person);
                    RepoPers.Save();

                    return(RedirectToAction(nameof(Index)));
                }
                return(View(viewModel));
            }
            catch (Exception)
            {
                return(View(viewModel));
            }
        }
        // GET: Items/Edit/5
        public IActionResult Edit(int id)
        {
            // we pass the current values into the Edit view
            // so that the input fields can be pre-populated instead of blank
            // (important for good UX)
            Domain.Model.Person person = RepoPers.GetPersonById(id);
            var viewModel = new PersonViewModel
            {
                PersonId  = person.Id,
                FirstName = person.FirstName,
                LastName  = person.LastName,
                Username  = person.Username,
                Password  = person.Password,
                Employee  = person.EmployeeTag,
                StoreName = RepoStore.GetStoreById(person.StoreId).Name,
            };
            List <string> myStores = new List <string> ();

            foreach (var val in RepoStore.GetStoresByName().ToList())
            {
                myStores.Add(val.Name);
            }
            ViewData["StoreName"] = new SelectList(myStores);
            return(View(viewModel));
        }
Ejemplo n.º 4
0
        public void UpdatePerson(Domain.Model.Person inputPerson)
        {
            _logger.LogInformation($"Updating person with ID {inputPerson.Id}");
            Context.Person currentEntity = _dbContext.People.Find(inputPerson.Id);
            Context.Person newEntity     = Mapper.UnMapPerson(inputPerson);

            _dbContext.Entry(currentEntity).CurrentValues.SetValues(newEntity);
        }
Ejemplo n.º 5
0
 public static Context.Person UnMapPerson(Domain.Model.Person person)
 {
     return(new Context.Person
     {
         PersonId = person.Id,
         FirstName = person.FirstName,
         LastName = person.LastName,
         Username = person.Username,
         Employee = person.EmployeeTag,
         Password = person.Password,
         StoreId = person.StoreId
     });
 }
Ejemplo n.º 6
0
        public void AddPerson(Domain.Model.Person inputPerson)
        {
            if (inputPerson.Id != 0)
            {
                _logger.LogWarning($"Person to be added has an ID ({inputPerson.Id}) already: ignoring.");
            }

            _logger.LogInformation("Adding person");

            Context.Person entity = Mapper.UnMapPerson(inputPerson);
            entity.PersonId = 0;
            _dbContext.Add(entity);
        }
        // GET: Items/Delete/5
        public IActionResult Delete(int id)
        {
            Domain.Model.Person person = RepoPers.GetPersonById(id);
            var viewModel = new PersonViewModel
            {
                PersonId  = person.Id,
                FirstName = person.FirstName,
                LastName  = person.LastName,
                Username  = person.Username,
                Password  = person.Password,
                Employee  = person.EmployeeTag,
                StoreName = RepoStore.GetStoreById(person.StoreId).Name,
            };

            return(View(viewModel));
        }