Пример #1
0
        [Authorize(gAppConst.AccessPolicies.LevelOne)]  /// Done
        public async Task <IActionResult> Delete([FromBody] gDepartment department)
        {
            try
            {
                /// if the Department record with the same id is not found
                if (!await DbContext.Departments.AnyAsync(d => d.Id == department.Id).ConfigureAwait(false))
                {
                    gAppConst.Error(ref ErrorsList, "Department not found");
                    return(BadRequest(ErrorsList));
                }


                /// If the department is in use by any user then do not allow delete
                if (await DbContext.Users.AnyAsync(u => u.Department.Id == department.Id).ConfigureAwait(false))
                {
                    gAppConst.Error(ref ErrorsList, "Category tag is in use by an idea.");
                    return(BadRequest(ErrorsList));
                }

                /// else the department is found
                /// now delete the department record
                DbContext.Departments.Remove(department);
                /// save the changes to the database
                await DbContext.SaveChangesAsync().ConfigureAwait(false);

                /// return 200 Ok status
                return(Ok($"Department '{department.Name}' was deleted"));
            }
            catch (Exception)
            {
                /// Add the error below to the error list and return bad Department
                gAppConst.Error(ref ErrorsList, $"Server Error. Please Contact Administrator.");
                return(BadRequest(ErrorsList));
            }
        }
Пример #2
0
        [Authorize(gAppConst.AccessPolicies.LevelOne)]  /// Done
        public async Task <IActionResult> Post([FromBody] gDepartment newDepartment)
        {
            try
            {
                /// if model validation failed
                if (!TryValidateModel(newDepartment))
                {
                    gAppConst.ExtractErrors(ModelState, ref ErrorsList);
                    /// return bad request with all the errors
                    return(BadRequest(ErrorsList));
                }

                /// check the database to see if a department with the same name exists
                if (await DbContext.Departments.AnyAsync(d => d.Name == newDepartment.Name).ConfigureAwait(false))
                {
                    /// extract the errors and return bad request containing the errors
                    gAppConst.Error(ref ErrorsList, "Department already exists.");
                    return(BadRequest(ErrorsList));
                }

                /// else department object is made without any errors
                /// Add the new department to the EF context
                await DbContext.Departments.AddAsync(newDepartment).ConfigureAwait(false);

                /// save the changes to the data base
                await DbContext.SaveChangesAsync().ConfigureAwait(false);

                /// return 201 created status with the new object
                /// and success message
                return(Created("Success", newDepartment));
            }
            catch (Exception) // DbUpdateException, DbUpdateConcurrencyException
            {
                /// Add the error below to the error list and return bad request
                gAppConst.Error(ref ErrorsList, "Server Error. Please Contact Administrator.");
                return(BadRequest(ErrorsList));
            }
        }