Пример #1
0
        public async Task <ActionResult> Create(FormCollection formCollection)
        {
            Core.ViewModels.Employee newEmployee = new Core.ViewModels.Employee
            {
                FirstName   = formCollection["firstName"],
                LastName    = formCollection["lastName"],
                PhoneNumber = formCollection["phoneNumber"],
                Address     = formCollection["address"],
                PostalCode  = formCollection["postalCode"],
                City        = formCollection["city"]
            };

            try
            {
                await repository.CreateEmployee(newEmployee);
            }
            catch (InvalidOperationException e)
            {
                return(Json(new { status = "error", message = e.Message }));
            }

            return(Json(new { status = "success", message = "Employee has been successfully created." }));
        }
        public async Task <JsonResult> CreateAndUpdateEmployee([FromBody]  CreateAndUpdate updateAndCreateEmployee)
        {
            var result = 0;

            if (updateAndCreateEmployee.Id != 0)
            {
                try
                {
                    var employee = new UpdateEmployee
                    {
                        Id           = updateAndCreateEmployee.Id,
                        FullName     = updateAndCreateEmployee.FullName,
                        PositionId   = updateAndCreateEmployee.PositionId,
                        DepartmentId = updateAndCreateEmployee.DepartmentId,
                        Sex          = updateAndCreateEmployee.Sex,
                        DOB          = updateAndCreateEmployee.DOB,
                        IdNumber     = updateAndCreateEmployee.IdNumber,
                        PhoneNumber  = updateAndCreateEmployee.PhoneNumber,
                        Email        = updateAndCreateEmployee.Email,
                        Address      = updateAndCreateEmployee.Address,
                        TaxId        = updateAndCreateEmployee.TaxId,
                        Image        = updateAndCreateEmployee.Image,
                        EditDate     = DateTime.Now
                    };
                    result = await _repository.UpdateEmployee(employee);
                }catch (Exception exp)
                {
                    throw exp;
                }
            }
            else
            {
                try
                {
                    var create = new CreateEmployee
                    {
                        FullName = updateAndCreateEmployee.FullName,

                        PositionId   = updateAndCreateEmployee.PositionId,
                        DepartmentId = updateAndCreateEmployee.DepartmentId,
                        Sex          = updateAndCreateEmployee.Sex,
                        DOB          = updateAndCreateEmployee.DOB,
                        IdNumber     = updateAndCreateEmployee.IdNumber,
                        PhoneNumber  = updateAndCreateEmployee.PhoneNumber,
                        Email        = updateAndCreateEmployee.Email,
                        Address      = updateAndCreateEmployee.Address,
                        TaxId        = updateAndCreateEmployee.TaxId,
                        Image        = updateAndCreateEmployee.Image
                    };
                    result = await _repository.CreateEmployee(create);
                }catch (Exception exp)
                {
                    throw exp;
                }
            }
            if (result > 0)
            {
                return(Json(new { status = 1, message = " successfully." }));
            }
            return(Json(new { status = 0, message = "Something went wrong, please contact administrator." }));
        }