public BaseResponse <YachtUpdateModel> GetYachtInfoYacht(int yachtId) { try { var yachtInfo = new YachtUpdateModel(); if (yachtId > 0) { var result = _context.Yachts.AsNoTracking().FirstOrDefault(k => k.Id == yachtId && k.Deleted == false); if (result != null) { var yachtPort = _context.YachtPorts.AsNoTracking() .Where(x => x.YachtFid == yachtId && x.Deleted == false && x.IsActivated == true && x.EffectiveDate <= DateTime.Now) .OrderByDescending(x => x.EffectiveDate) .FirstOrDefault(); yachtInfo = new YachtUpdateModel(); yachtInfo.InjectFrom(result); if (yachtPort != null) { yachtInfo.PortLocationId = yachtPort.PortFid; } return(BaseResponse <YachtUpdateModel> .Success(yachtInfo)); } } return(BaseResponse <YachtUpdateModel> .Success(yachtInfo)); } catch (Exception ex) { return(BaseResponse <YachtUpdateModel> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } }
public IActionResult UpdateYachtInfo(YachtUpdateModel model) { if (model.Id < 0 && model == null) { return(BadRequest()); } var result = _yachtService.UpdateYachtInfo(model); if (result.IsSuccessStatusCode) { return(Ok(result)); } return(BadRequest()); }
public BaseResponse <bool> UpdateYachtInfo(YachtUpdateModel model) { using (var trans = _context.Database.BeginTransaction()) { try { var userId = GetUserGuidId(); //Update yacht basic info var yachtInfo = _context.Yachts.FirstOrDefault(x => x.Id == model.Id); if (yachtInfo == null) { return(BaseResponse <bool> .BadRequest(false)); } yachtInfo = _mapper.Map <YachtUpdateModel, Yachts>(model, yachtInfo); yachtInfo.LastModifiedDate = DateTime.Now; yachtInfo.LastModifiedBy = userId; _context.Yachts.Update(yachtInfo); _context.SaveChanges(); //Update yacht port var yachtPort = _context.YachtPorts.AsNoTracking().Where(x => x.YachtFid == yachtInfo.Id && x.PortFid == model.PortLocationId) .OrderByDescending(x => x.EffectiveDate) .FirstOrDefault(); var portInfo = _context.PortLocations.Where(x => x.Id == model.PortLocationId && x.Deleted == false).FirstOrDefault(); if (yachtPort != null) { //Has been port if (yachtPort.Deleted == true) { yachtPort.Deleted = false; } if (yachtPort.IsActivated == false) { yachtPort.IsActivated = true; } if (yachtPort.EffectiveDate != DateTime.Now) { yachtPort.EffectiveDate = DateTime.Now; } yachtPort.LastModifiedDate = DateTime.Now; yachtPort.LastModifiedBy = userId; _context.YachtPorts.Update(yachtPort); } else { //Add new port var newYachtPort = new YachtPorts { PortName = portInfo.PickupPointName, YachtFid = yachtInfo.Id, PortFid = model.PortLocationId, EffectiveDate = DateTime.Now, Deleted = false, IsActivated = true, CreatedBy = userId, CreatedDate = DateTime.Now }; _context.YachtPorts.Add(newYachtPort); } //Update new location yachtInfo.Country = portInfo.Country; yachtInfo.City = portInfo.City; _context.Yachts.Update(yachtInfo); _context.SaveChanges(); trans.Commit(); trans.Dispose(); return(BaseResponse <bool> .Success(true)); } catch (Exception ex) { trans.Rollback(); trans.Dispose(); return(BaseResponse <bool> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } } }