public async Task <TherapyMain> UpdateTherapyMain(string type, TherapyMain therapyMain) { if (!type.Equals(therapyMain.Type)) { throw new TherapyMainTypesDoNotMatchException(); } if (!await TherapyMainExistsByType(type)) { throw new TherapyMainDoesNotExistException(); } var local = await _context.TherapyMain.FindAsync(type); if (local == null) { throw new TherapyMainDoesNotExistException(); } UpdateNonNullAndNonEmptyFields(local, therapyMain); _context.Entry(local).State = EntityState.Modified; //_context.Entry(therapyMain).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(therapyMain); }
public async Task <TherapyMain> AddTherapyMain(TherapyMain therapyMain) { if (await TherapyMainExistsByType(therapyMain.Type)) { throw new TherapyMainTypeAlreadyExistsException(); } if (await TherapyMainExistsByAbbreviation(therapyMain.Abbreviation)) { throw new TherapyMainTypeAbbreviationAlreadyExistsException(); } therapyMain.Active = true; _context.TherapyMain.Add(therapyMain); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { throw; } return(therapyMain); }
public void Initialize() { var options = new DbContextOptionsBuilder <CoreDbContext>() .UseInMemoryDatabase(databaseName: "TherapyMainDatabase") .Options; _testTherapyMains = new List <TherapyMain>(); _testContext = new CoreDbContext(options); _testContext.Database.EnsureDeleted(); for (var i = 0; i < 10; i++) { var newTherapyMain = ModelFakes.TherapyMainFake.Generate(); _testContext.Add(newTherapyMain); _testContext.SaveChanges(); _testTherapyMains.Add(ObjectExtensions.Copy(_testContext.TherapyMain.FirstOrDefault(t => t.Type.Equals(newTherapyMain.Type)))); } _nonActiveTherapyMain = ModelFakes.TherapyMainFake.Generate(); _nonActiveTherapyMain.Active = false; _testContext.Add(_nonActiveTherapyMain); _testContext.SaveChanges(); _testTherapyMains.Add(ObjectExtensions.Copy(_testContext.TherapyMain.FirstOrDefault(t => t.Type.Equals(_nonActiveTherapyMain.Type)))); _testTherapyMainService = new TherapyMainService(_testContext); _testTherapyMainController = new TherapyMainController(_testTherapyMainService); }
private void UpdateNonNullAndNonEmptyFields(TherapyMain local, TherapyMain therapyMain) { foreach (PropertyInfo prop in typeof(TherapyMain).GetProperties()) { if (prop.GetValue(therapyMain) != null && (prop.PropertyType != typeof(string) || !prop.GetValue(therapyMain).Equals(""))) { prop.SetValue(local, prop.GetValue(therapyMain)); } } }
public async Task <IActionResult> PutTherapyMain(string type, TherapyMain therapyMain) { try { await _therapyMainService.UpdateTherapyMain(type, therapyMain); } catch (TherapyMainTypesDoNotMatchException e) { return(BadRequest(e)); } catch (TherapyMainDoesNotExistException) { return(NotFound()); } catch (DbUpdateConcurrencyException) { throw; } return(NoContent()); }
public async Task <ActionResult <TherapyMain> > PostTherapyMain(TherapyMain therapyMain) { try { await _therapyMainService.AddTherapyMain(therapyMain); } catch (TherapyMainTypeAlreadyExistsException e) { return(Conflict(e)); } catch (TherapyMainTypeAbbreviationAlreadyExistsException e) { return(Conflict(e)); } catch (DbUpdateException) { throw; } return(CreatedAtAction("GetTherapyMain", new { id = therapyMain.Type }, therapyMain)); }