Exemplo n.º 1
0
 public HttpResponseMessage Create(HttpRequestMessage request, ImportViewModel importViewModel)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             var newImportModel = new Import();
             var newImportDetailModel = new ImportDetail();
             var identity = (ClaimsIdentity)User.Identity;
             IEnumerable <Claim> claims = identity.Claims;
             newImportModel.UpdateImport(importViewModel);
             _importService.Add(newImportModel);
             _importService.Save();
             foreach (var item in importViewModel.ImportDetails)
             {
                 newImportDetailModel.UpdateImportDetail(item);
                 _importDetailService.Add(newImportDetailModel);
                 _importDetailService.Save();
             }
             Log log = new Log()
             {
                 AppUserId = claims.FirstOrDefault().Value,
                 Content = Notification.CREATE_IMPORT,
                 Created = DateTime.Now
             };
             _logService.Create(log);
             _logService.Save();
             var responseData = Mapper.Map <Import, ImportViewModel>(newImportModel);
             response = request.CreateResponse(HttpStatusCode.OK, responseData);
         }
         return response;
     }));
 }
Exemplo n.º 2
0
        public HttpResponseMessage Create(HttpRequestMessage request, ImportViewModel importViewModel)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var newImport = new Import();
                    newImport.UpdateImport(importViewModel);
                    newImport.CreatedDate = DateTime.Now;
                    newImport.CreatedBy = User.Identity.Name;
                    _importService.Add(newImport);
                    _importService.Save();
                    //if(importDetailViewModels != null)
                    //{
                    //    var newImportDetail = new ImportDetail();
                    //    var listImportDetailViewModels = new JavaScriptSerializer().Deserialize<List<ImportDetailViewModel>>(importDetailViewModels);
                    //    foreach (var importDetailViewModel in listImportDetailViewModels)
                    //    {
                    //        newImportDetail.UpdateImportDetail(importDetailViewModel);
                    //        newImportDetail.ImportID = newImport.ID;
                    //        _importDetailService.Add(newImportDetail);
                    //        _importDetailService.Save();
                    //    }
                    //}

                    var responseData = Mapper.Map <Import, ImportViewModel>(newImport);
                    response = request.CreateResponse(HttpStatusCode.Created, responseData);
                }

                return response;
            }));
        }