public async Task <IActionResult> PostOrganizationEmployees([FromBody] OrganizationEmployees organizationEmployees) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.OrganizationEmployees.Add(organizationEmployees); await _context.SaveChangesAsync(); return(CreatedAtAction("GetOrganizationEmployees", new { id = organizationEmployees.Id }, organizationEmployees)); }
public async Task <IActionResult> JoinOrganization([FromBody] JObject newData) { int userID; int organizationID; var secretOrganizationPassword = newData["organizationPassword"].ToString(); int.TryParse(newData["userID"].ToString(), out userID); int.TryParse(newData["organizationID"].ToString(), out organizationID); if (_context.Users.Any(tempUser => tempUser.Id == userID) == false) { var currError = new { error = "This user does not exist." }; return(new JsonResult(currError)); } else if (_context.Organizations.Any(tempOrg => tempOrg.Id == organizationID) == false) { var currError = new { error = "This organization does not exist." }; return(new JsonResult(currError)); } else if (_context.OrganizationEmployees.Any(tempOrgEmployee => tempOrgEmployee.UserId == userID && tempOrgEmployee.OrganizationId == organizationID)) { var currError = new { error = "You are already an employee." }; return(new JsonResult(currError)); } OrganizationPasswords currOrganization = await _context.OrganizationPasswords.Where(tempOrg => tempOrg.OrganizationId == organizationID).FirstOrDefaultAsync(); if (currOrganization == null) { var currError = new { error = "You are not able to join this organization as it is private." }; return(new JsonResult(currError)); } if (currOrganization.Password != secretOrganizationPassword) { var currError = new { error = "Incorrect password, please contact your manager." }; return(new JsonResult(currError)); } OrganizationEmployees newOrgEmployee = new OrganizationEmployees(); newOrgEmployee.UserId = userID; newOrgEmployee.OrganizationId = organizationID; _context.OrganizationEmployees.Add(newOrgEmployee); await _context.SaveChangesAsync(); Organizations retOrg = await _context.Organizations.Where(tempOrg => tempOrg.Id == organizationID).FirstOrDefaultAsync(); var response = new { joinOrganization = retOrg, OrganizationEmployees = newOrgEmployee }; return(new JsonResult(response)); }
public async Task <IActionResult> PostOrganizations([FromBody] JObject data) { // Convert JSON organization object from data Organizations newOrg = JsonConvert.DeserializeObject <Organizations>(data["Organization"].ToString()); int userId = JsonConvert.DeserializeObject <int>(data["User"].ToString()); //Error for invalid User if (_context.Users.Any(newUser => newUser.Id == userId) == false) { return(NotFound()); } //Save organization first to get organization ID required for further tables _context.Organizations.Add(newOrg); await _context.SaveChangesAsync(); OrganizationManagers newOrgManager = new OrganizationManagers(); newOrgManager.OrganizationId = newOrg.Id; newOrgManager.UserId = userId; OrganizationEmployees newOrgEmployee = new OrganizationEmployees(); newOrgEmployee.UserId = userId; newOrgEmployee.OrganizationId = newOrg.Id; OrganizationPasswords newOrgPassword = new OrganizationPasswords(); newOrgPassword.OrganizationId = newOrg.Id; newOrgPassword.Password = generatePassword(); _context.OrganizationManagers.Add(newOrgManager); _context.OrganizationEmployees.Add(newOrgEmployee); _context.OrganizationPasswords.Add(newOrgPassword); await _context.SaveChangesAsync(); var responseData = new { newOrganization = newOrg, newOrgEmployee = newOrgEmployee, newOrgManager = newOrgManager }; return(new JsonResult(responseData)); }
public async Task <IActionResult> PutOrganizationEmployees([FromRoute] int id, [FromBody] OrganizationEmployees organizationEmployees) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != organizationEmployees.Id) { return(BadRequest()); } _context.Entry(organizationEmployees).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OrganizationEmployeesExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }