Exemplo n.º 1
0
        public async Task <IActionResult> UpdateRange(int id, [FromBody] RangeIpAddressModel range)
        {
            if (ModelState.IsValid)
            {
                if (range == null)
                {
                    return(BadRequest($"nameof(range) cannot be found"));
                }

                if (id != range.RangeId)
                {
                    return(BadRequest("Conflicting range id in parameter"));
                }

                TRangeIpAddress rangeApp = _unitOfWork.RangeIpAddresses.Get(id);
                if (rangeApp == null)
                {
                    return(NotFound(id));
                }

                /*var config=new MapperConfiguration(cfg=>cfg.CreateMap<RangeIpAddressModel,TRangeIpAddress>());
                 * var mapper = config.CreateMapper();*/
                Mapper.Map <RangeIpAddressModel, TRangeIpAddress>(range, rangeApp);

                _unitOfWork.RangeIpAddresses.Update(rangeApp);
                _unitOfWork.SaveChanges();

                return(NoContent());
            }

            return(BadRequest(ModelState));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> NewRange([FromBody] RangeIpAddressModel range)
        {
            if (ModelState.IsValid)
            {
                if (range == null)
                {
                    return(BadRequest($"nameof(range) cannot be found"));
                }


#if DEBUG
                Debug.WriteLine("range ->" + range.RangeId.ToString() + " " + range.StartAddress.ToString());
#endif
//                var config=new MapperConfiguration(cfg=>cfg.CreateMap<RangeIpAddressModel,TRangeIpAddress>());
                TRangeIpAddress rangeApp = new TRangeIpAddress(

                    /*{
                     *  RangeId = 0
                     * };*/
                    );
//                var mapper = config.CreateMapper();
                Mapper.Map <RangeIpAddressModel, TRangeIpAddress>(range, rangeApp);
#if DEBUG
                Debug.WriteLine("rangeApp ->" + rangeApp.RangeId.ToString() /*+" "+rangeApp.StartAddress.ToString()*/);
#endif


                _unitOfWork.RangeIpAddresses.Add((rangeApp));
                _unitOfWork.SaveChanges();

                /*config=new MapperConfiguration(cfg=>cfg.CreateMap<TRangeIpAddress,RangeIpAddressModel>());
                 * mapper = config.CreateMapper();*/

                var newRange = _unitOfWork.RangeIpAddresses.GetAll().OrderByDescending(o => o.RangeId).FirstOrDefault();
                #if DEBUG
                Debug.WriteLine("newRange ->" + newRange.RangeId.ToString() + " " + newRange.StartAddress.ToString());
                #endif

                return(Ok(Mapper.Map <TRangeIpAddress, RangeIpAddressModel>(newRange)));
            }

            return(BadRequest(ModelState));
        }