public async Task <Enums.ServiceResult> AddWolfToPack(WolfPackForModificationModel wolfPackForModification) { // Try to get the wolf and pack var wolf = await _dbContext.Wolfs.FirstOrDefaultAsync(w => w.Id == wolfPackForModification.WolfId); var pack = await _dbContext.Packs.FirstOrDefaultAsync(w => w.Id == wolfPackForModification.PackId); if (wolf == null || pack == null) { return(Enums.ServiceResult.NotFound); } // Check if relationship already exists // If it does its ok to just say it was created var existingWolfPack = await _dbContext.WolfPack.FirstOrDefaultAsync(w => w.WolfId == wolfPackForModification.WolfId && w.PackId == wolfPackForModification.PackId); if (existingWolfPack != null) { return(Enums.ServiceResult.Ok); } var wolfPack = new WolfPack { WolfId = wolfPackForModification.WolfId, PackId = wolfPackForModification.PackId }; _dbContext.Add(wolfPack); await _dbContext.SaveChangesAsync(); return(Enums.ServiceResult.Ok); }
public async Task <Enums.ServiceResult> RemoveWolfFromPack(WolfPackForModificationModel wolfPackForModification) { //Try to get the WolfPack var wolfPack = await _dbContext.WolfPack.FirstOrDefaultAsync(w => w.WolfId == wolfPackForModification.WolfId && w.PackId == wolfPackForModification.PackId); if (wolfPack == null) { return(Enums.ServiceResult.NotFound); } _dbContext.Remove(wolfPack); await _dbContext.SaveChangesAsync(); return(Enums.ServiceResult.Ok); }
public async Task <IActionResult> DeleteWolf( [FromBody] WolfPackForModificationModel wolfPackForModificationModel) { var serviceResult = await _wolfPackService.RemoveWolfFromPack(wolfPackForModificationModel); switch (serviceResult) { case Enums.ServiceResult.NotFound: return(NotFound()); case Enums.ServiceResult.Ok: default: return(Ok()); } }