public IActionResult Delete(int id) { var user = GetCurrentUserProfile(); var goal = _goalRepository.GetById(id); if (user.Id != goal.UserProfileId) { return(Forbid()); } _goalRepository.Delete(id); return(NoContent()); }
public IActionResult Delete(long id) { try { var item = _goalRepository.Find(id); if (item == null || item.User.Email != User.FindFirstValue(ClaimTypes.Email)) { return(NotFound(ErrorCode.DataProvidedIsInvalid.ToString())); } _goalRepository.Delete(id); } catch (Exception) { return(BadRequest(ErrorCode.CouldNotDeleteItem.ToString())); } return(NoContent()); }
public void GoalRepository() { Mock <IDbSetFactory> factory = new Mock <IDbSetFactory>(); Mock <DbSet <Goal> > dbSet = new Mock <DbSet <Goal> >(); factory.Setup(m => m.CreateDbSet <Goal>()).Returns(dbSet.Object); GoalRepository repo = new GoalRepository(factory.Object); var Goal = new Goal(); var sequence = new MockSequence(); dbSet.InSequence(sequence).Setup(e => e.Add(Goal)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); repo.Create(Goal); repo.Get(Goal.Id); repo.Update(Goal); repo.Delete(Goal.Id); }
public void Can_Delete() { //Prepare var goalRepository = new GoalRepository(); const int existedGoalId = 1; var goal = new Goal { Id = existedGoalId }; goalRepository.Add(goal); //Pre-Validate var existedGoal = goalRepository.Goals.FirstOrDefault(e => e.Id == existedGoalId); Assert.NotNull(existedGoal); Assert.Equal(existedGoalId, existedGoal.Id); //Perform goalRepository.Delete(goal); //Post-Validate Assert.DoesNotContain(goalRepository.Goals, e => e.Id == existedGoalId); }
public void CreateUpdateDeleteFetch() { // file system var path = "/shop/randomsilo/modern-web/backends/GoalTracker"; var project = "GoalTracker"; var outputPath = $"{path}/{project}.Infrastructure.Test/TestOutput/"; var databaseFile = GetDatabase(outputPath, MethodBase.GetCurrentMethod()); // logger ILogger logger = GetLogger($"{outputPath}/{MethodBase.GetCurrentMethod().ReflectedType.Name}_{MethodBase.GetCurrentMethod().Name}.log"); // database setup // - context IDatabaseContext databaseContext = new DatabaseContext( $"Data Source={databaseFile}" , "TestDb" , "TestSchema" , $"{path}/sql/sqlite/ALL.sql" ); Assert.NotNull(databaseContext); // - manager IManageDatabase databaseManager = new DatabaseManager(databaseContext); Assert.NotNull(databaseManager); // - create tables databaseManager.CreateDatabase(); // - repository var goalRepository = new GoalRepository(databaseManager, new GoalRepositorySql(), logger); Assert.NotNull(goalRepository); // faker BrashActionResult <Goal> result = null; var goalFaker = new GoalFaker(databaseManager, logger); Assert.NotNull(goalFaker); // create var goalCreateModel = goalFaker.GetOne(); result = goalRepository.Create(goalCreateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalId > 0); // use model with id goalCreateModel = result.Model; // update var goalUpdateModel = goalFaker.GetOne(); goalUpdateModel.GoalId = goalCreateModel.GoalId; result = goalRepository.Update(goalUpdateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); // delete result = goalRepository.Delete(goalCreateModel); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); // fetch // - make fakes var fakes = goalFaker.GetMany(10); // - add fakes to database List <int?> ids = new List <int?>(); foreach (var f in fakes) { result = goalRepository.Create(f); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalId >= 0); ids.Add(result.Model.GoalId); } // - get fakes from database foreach (var id in ids) { var model = new Goal() { GoalId = id }; result = goalRepository.Fetch(model); Assert.True(result.Status == BrashActionStatus.SUCCESS, result.Message); Assert.True(result.Model.GoalId >= 0); } }
public void GoalRepository() { Mock<IDbSetFactory> factory = new Mock<IDbSetFactory>(); Mock<DbSet<Goal>> dbSet = new Mock<DbSet<Goal>>(); factory.Setup(m => m.CreateDbSet<Goal>()).Returns(dbSet.Object); GoalRepository repo = new GoalRepository(factory.Object); var Goal = new Goal(); var sequence = new MockSequence(); dbSet.InSequence(sequence).Setup(e => e.Add(Goal)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); dbSet.InSequence(sequence).Setup(e => e.Find(Goal.Id)); repo.Create(Goal); repo.Get(Goal.Id); repo.Update(Goal); repo.Delete(Goal.Id); }