public IActionResult CreaterUser([FromBody] CreateUserRequest createUserRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!Enum.TryParse(createUserRequest.Role, out UserRoles userRole)) { return(BadRequest($"Role {createUserRequest.Role} does not exist")); } var department = _dbContext.Departments.First(x => x.Id == createUserRequest.DepartmentId); if (userRole != UserRoles.Admin && department == null) { return(BadRequest($"Dpartment {createUserRequest.DepartmentId} does not exist")); } if (_dbContext.Users.Any(u => u.Login == createUserRequest.Login)) { return(BadRequest($"Login {createUserRequest.Login} is not unique")); } if (string.IsNullOrWhiteSpace(createUserRequest.Login)) { return(BadRequest($"User login is empty")); } var dbUser = new User { Login = createUserRequest.Login, Password = createUserRequest.Pass, Role = (int)userRole, Department = department }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Users.Add(dbUser); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbUser.UserToWebUser())); }
private void AddWorkStatus(object sender, RoutedEventArgs e) { if (EmployeeIDBox.Text.Length == 0 || monthBox.Text.Length == 0 || yearBox.Text.Length == 0 || MaxHoursBox.Text.Length == 0 || ActualHoursBox.Text.Length == 0 || MaxSalaryBox.Text.Length == 0) { MessageBox.Show("Fill all fields!"); } else { using (AccountingSystemContext repository = new AccountingSystemContext()) { Months currentMonth = (Months)Enum.Parse(typeof(Months), monthBox.Text); int currentEmployeeId = Int32.Parse(EmployeeIDBox.Text); int currentYear = Int32.Parse(yearBox.Text); int currentMaxHours = Int32.Parse(MaxHoursBox.Text); int currentMaxSalary = Int32.Parse(MaxSalaryBox.Text); int currentActualHours = Int32.Parse(ActualHoursBox.Text); var currentEmployee = repository.Employees.First(x => x.ID == currentEmployeeId); currentWorkHours = new WorkHours(currentEmployee, currentMonth, currentYear, currentMaxHours, currentMaxSalary, currentActualHours); repository.WorkHourses.Add(currentWorkHours); repository.SaveChanges(); } ActualSalaryBlock.Text = currentWorkHours.ActualSalary.ToString(); } }
/// <summary> /// Creates new employee /// </summary> /// <param name="employee">Create employee request</param> public WebEmployee CreateEmployee(CreateEmployeeRequest employee) { var dbDepartment = _dbContext.Departments.First(d => d.Id == employee.DepartmentId); if (dbDepartment == null) { throw new NotFoundException($"Department {employee.DepartmentId} does not exist"); } var dbSalaryInfo = _dbContext.Salaries.First(d => d.Id == employee.SalaryId); if (dbSalaryInfo == null) { throw new NotFoundException($"Salary info {employee.DepartmentId} does not exist"); } var dbEmployee = new Employee { FirstName = employee.FirstName, SecondName = employee.SecondName, Address = employee.Address, Department = dbDepartment, DepartmentId = employee.DepartmentId, SalaryInfo = dbSalaryInfo, SalaryInfoId = employee.SalaryId, }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Employees.Add(dbEmployee); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(dbEmployee.EmployeeToWebEmployee()); }
public IActionResult CreateSalary([FromBody] CreateSalaryInfoRequest createSalaryInfoRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!Enum.TryParse(createSalaryInfoRequest.Type, out SalaryType salaryType)) { return(BadRequest($"Salary type {createSalaryInfoRequest.Type} does not exist")); } if (!Enum.TryParse(createSalaryInfoRequest.PaymentType, out PaymentType paymentType)) { return(BadRequest($"Salary type {createSalaryInfoRequest.Type} does not exist")); } var dbSalaryInfo = new SalaryInfo { Type = (int)salaryType, PaymentType = (int)paymentType, Rate = createSalaryInfoRequest.Rate, Salary = createSalaryInfoRequest.Salary, BankAccount = createSalaryInfoRequest.BankAccount }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Salaries.Add(dbSalaryInfo); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbSalaryInfo.SalaryInfoToWebSalaryInfo())); }
public IActionResult CreaterTimeCard([FromBody] CreateTimeCardRequest createTimeCardRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbEmployee = _dbContext.Employees.First(e => e.Id == createTimeCardRequest.EmployeeId); if (dbEmployee == null) { return(BadRequest($"Employee {createTimeCardRequest.EmployeeId} does not exist")); } var dbTimeCard = new TimeCard { Comment = createTimeCardRequest.Comment, Time = createTimeCardRequest.Time, EmployeeId = createTimeCardRequest.EmployeeId, Employee = dbEmployee }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.TimeCards.Add(dbTimeCard); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbTimeCard.TimeCardToWebTimeCard())); }
public IActionResult CreateDepartment([FromBody] CreateDepartmentRequest createDepartmentRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (_dbContext.Departments.Any(u => u.Name == createDepartmentRequest.Name)) { return(BadRequest($"Name {createDepartmentRequest.Name} is not unique")); } if (string.IsNullOrEmpty(createDepartmentRequest.Name)) { return(BadRequest($"Department name is empty")); } var dbDepartment = new Department { Name = createDepartmentRequest.Name }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Departments.Add(dbDepartment); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } return(Ok(dbDepartment.DepartmentToWebDepartment())); }
private void AddNewEmpl(object sender, RoutedEventArgs e) { if (FirstNameBox.Text.Length == 0 || LastNameBox.Text.Length == 0 || StartDateBox.Text.Length == 0) { MessageBox.Show("Fill all fields!"); } else { using (AccountingSystemContext repository = new AccountingSystemContext()) { repository.Employees.Add(new Employee(FirstNameBox.Text, LastNameBox.Text, StartDateBox.Text)); repository.SaveChanges(); logger.Info($"User [{FirstNameBox.Text}] [{LastNameBox.Text}] has been added!"); } MainWindow main = new MainWindow(); main.Show(); Close(); } }
public IActionResult RegisterUser([FromBody] AuthRequest user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbUser = new User { Login = user.Login, Password = user.Pass, DepartmentId = Const.DEFAULT_DEPARTMENT_ID }; using (var txn = _dbContext.Database.BeginTransaction()) { try { _dbContext.Users.Add(dbUser); _dbContext.SaveChanges(); txn.Commit(); } catch { txn.Rollback(); throw; } } var authResult = GetToken(new AuthRequest { Login = user.Login, Pass = user.Pass }); return(Created($"user/{dbUser.Id.ToString()}", ((Microsoft.AspNetCore.Mvc.OkObjectResult)authResult).Value)); }