Exemple #1
0
        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)
     });
 }
Exemple #3
0
        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);
        }
Exemple #7
0
 // PUT api/<controller>/5
 public void Put(int id, [FromBody] TerritoryDto territory)
 {
     _manager.PutTerritory(id, territory.ToDto <Territory>());
 }
Exemple #8
0
 // POST api/<controller>
 public void Post([FromBody] TerritoryDto territory)
 {
     _manager.PostTerritory(territory.ToDto <Territory>());
 }
Exemple #9
0
 public IActionResult Put(int id, [FromBody] TerritoryDto dto)
 {
     TerritoryService.Update(id, dto);
     return(Ok());
 }
Exemple #10
0
 public IActionResult Post([FromBody] TerritoryDto dto)
 {
     return(Ok(new { id = TerritoryService.Create(dto) }));
 }