/// <summary> /// Method gets catalog type object by Id /// </summary> /// <param name="id">Id of an catalog type that we want to get</param> /// <returns>Catalog type object</returns> public async Task <CatalogType> GetCatalogTypeById(Guid id) { var catalogType = await DB.CatalogTypeDB.FirstOrDefaultAsync(x => x.Id == id); StorageException.ThrowNotFoundIfNull(catalogType); return(catalogType); }
/// <summary> /// Method that deletes catalog type from DB /// </summary> /// <param name="catalogTypeId">Catalog type id that we want to delete</param> /// <param name="userId">Id of an user that want to delete this catalog type</param> public async Task DeleteCatalogType(Guid catalogTypeId, string userId) { // Getting catalog that we want to delete var catalogToDelete = await DB.CatalogTypeDB.FirstOrDefaultAsync(x => x.Id == catalogTypeId); // Getting user that wants to delete catalog type var userThatEditedCatalogType = await DB.Users.FirstOrDefaultAsync(x => x.Id == userId); // Checking objects that we got from DB on null StorageException.ThrowNotFoundIfNull(catalogToDelete); StorageException.ThrowNotFoundIfNull(userThatEditedCatalogType); // Checking if catalog that is going to be deleted attached/related with him or hes boss, if it's related then we deleteit from DB if (catalogToDelete.UserId == userId || catalogToDelete.UserId == userThatEditedCatalogType.ReportsTo) { // If there are still catalog names in DB that are related to this catalog type, then we wont allow it to be deleted if (catalogToDelete.Amount != 0) { throw new StorageException("Vēl ir katalogu nosaukumi kuri ir saistīti ar šo tīpu"); } // Deleting catalog type if all is okey DB.CatalogTypeDB.Remove(catalogToDelete); await DB.SaveChangesAsync(); } // If user managed some how to send delete request, that is attached to other workers, then we will send a excetion with message else { throw new Exception("Jūms nāv tiesības lai nodzēst katalogu tipu."); } }
// All base crud methods are stored here #region Base CRUD /// <summary> /// Method gets catalog types from DB that has created a specific user /// </summary> /// <param name="userId">Id of an user that has created catalog types</param> /// <returns>List of catalog </returns> public async Task <List <CatalogType> > GetAllCatalogTypes(string userId) { var user = await DB.Users.FirstOrDefaultAsync(x => x.Id == userId); StorageException.ThrowNotFoundIfNull(user); if (await userManager.IsInRoleAsync(user, StorageConstants.Role_FourthLevel)) { var userLevelThreeBoss = await DB.Users.FirstOrDefaultAsync(x => x.Id == user.ReportsTo); StorageException.ThrowNotFoundIfNull(userLevelThreeBoss); return(await DB.CatalogTypeDB .Where(x => x.UserId == userLevelThreeBoss.ReportsTo) .ToListAsync()); } return(await DB.CatalogTypeDB .Where(x => x.UserId == userId || x.UserId == user.ReportsTo) .ToListAsync()); }