public object Create(TerritoryDto dto) { var entity = Mapper.Map <Territory>(dto); context.Add(entity); context.SaveChanges(); return(entity.TerritoryId); }
private static Territory MapTo(this TerritoryDto dto) { return(new() { TerritoryID = dto.TerritoryID, TerritoryDescription = dto.TerritoryDescription, RegionID = dto.RegionID, Region = MapTo(dto.Region) }); }
public void Update(int id, TerritoryDto dto) { var entity = context.Territories.Find(id); if (entity == null) { throw new NotFoundException(); } context.Update(Mapper.Map(dto, entity)); context.SaveChanges(); }
public void CanCreateDtoWithEntity() { Territory territory = CreateTerritory(); TerritoryDto territoryDto = TerritoryDto.Create(territory); territoryDto.Id.ShouldEqual("08837"); territoryDto.Description.ShouldEqual("Edison"); territoryDto.RegionBelongingTo.ShouldNotBeNull(); territoryDto.RegionBelongingTo.Id.ShouldEqual(1);; territoryDto.Employees.Count.ShouldEqual(2); territoryDto.Employees[0].Id.ShouldEqual(5); territoryDto.Employees[1].Id.ShouldEqual(10); }
public IActionResult Post([FromBody] TerritoryDto dto) { var territoriesRepository = _unitOfWork.TerritoriesRepository; if (dto.ParentId.HasValue && territoriesRepository.Exists(dto.ParentId.Value, dto.Name)) { return(BadRequest()); } var territory = new Territory { ParentId = dto.ParentId, Name = dto.Name }; territoriesRepository.Add(territory); _unitOfWork.Save(); return(Ok(_territoryDtoMapper.Map(territory))); }
public IList <TerritoryDto> GetTerritories() { // I'd rather have the transaction begun via an attribute, like with a controller action, // or within a service object, but this works for the current example. this.territoryRepository.DbContext.BeginTransaction(); var territories = this.territoryRepository.GetAll(); var territoryDtos = new List <TerritoryDto>(); foreach (var territory in territories) { territoryDtos.Add(TerritoryDto.Create(territory)); } // Since we're certainly not going to require lazy loading, commit the transcation // before returning the data. this.territoryRepository.DbContext.CommitTransaction(); return(territoryDtos); }
// PUT api/<controller>/5 public void Put(int id, [FromBody] TerritoryDto territory) { _manager.PutTerritory(id, territory.ToDto <Territory>()); }
// POST api/<controller> public void Post([FromBody] TerritoryDto territory) { _manager.PostTerritory(territory.ToDto <Territory>()); }
public IActionResult Put(int id, [FromBody] TerritoryDto dto) { TerritoryService.Update(id, dto); return(Ok()); }
public IActionResult Post([FromBody] TerritoryDto dto) { return(Ok(new { id = TerritoryService.Create(dto) })); }