Exemplo n.º 1
0
        public JsonResult Post([FromBody] VesselViewModel vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newVessel = vm.ToEntity();
                    _logger.LogInformation("Attempting to save a new Vessel");

                    _repository.Add(newVessel);

                    _unitOfWork.Commit();

                    Response.StatusCode = (int)HttpStatusCode.Created;
                    return(Json(VesselViewModel.FromEntity(newVessel)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save Vessel", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Failed to save Vessel"));
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
Exemplo n.º 2
0
        public async Task <Data.Vessel> AddOrUpdateVesselAsync(VesselViewModel model)
        {
            if (model == null)
            {
                throw new NullReferenceException("VesselViewModel is null");
            }

            Data.Vessel entity   = null;
            var         existing = new List <Data.Vessel>();

            // няма търсене по уникален номер на кораб в Regix
            //string identifier = model.RegistrationData != null ? model.RegistrationData.RegistrationNumber : "";
            //var existing = await _context.Vessel
            //    .Include(x => x.VesselEngine)
            //    .Include(x => x.VesselOwner)
            //    .Include(x => x.VesselRegistrationData)
            //    .Include(x => x.VesselRegistrationData).ThenInclude(x => x.StatusNavigation)
            //    .Where(x => x.VesselRegistrationData.Any() &&
            //        x.VesselRegistrationData.Where(a => a.RegistrationNumber.ToLower() == identifier.ToLower()).Any() &&
            //        x.VesselExtension != null &&
            //        x.VesselExtension.Deactivated == false)
            //    .ToListAsync();

            if (!existing.Any())
            {
                entity    = model.ToEntity();
                entity.Id = 0;
                entity.VesselExtension.VesselId = entity.Id;
                await _context.Vessel.AddAsync(entity);
            }
            else
            {
                foreach (Data.Vessel item in existing)
                {
                    item.VesselExtension.Deactivated = true;
                    _context.VesselEngine.RemoveRange(item.VesselEngine);
                    _context.VesselOwner.RemoveRange(item.VesselOwner);
                    _context.VesselRegistrationData.RemoveRange(item.VesselRegistrationData);
                }

                existing[0].UpdateEntity(model);
                existing[0].VesselExtension.Deactivated = false;
                existing[0].VesselExtension.UpdatedAt   = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
                existing[0].VesselExtension.RequestId   = model.ExtensionRequestId;

                _context.Vessel.Update(existing[0]);
                entity = existing[0];
            }

            await _context.SaveChangesAsync();

            return(entity);
        }