public BaseResponse <bool> CreateYacht(YachtCreateModel model) { using (var transaction = _context.Database.BeginTransaction()) { try { var userId = GetUserGuidId(); //Add yacht var yacht = new Yachts(); yacht = _mapper.Map <YachtCreateModel, Yachts>(model, yacht); yacht.UniqueId = UniqueIDHelper.GenarateRandomString(12); yacht.Deleted = false; yacht.CreatedBy = userId; yacht.CreatedDate = DateTime.Now; yacht.LastModifiedBy = userId; yacht.LastModifiedDate = DateTime.Now; _context.Yachts.Add(yacht); var result = _context.SaveChanges(); var listYachtAttrVal = new List <YachtAttributeValues>(); //Add yacht attributevalue accomodations if (!string.IsNullOrEmpty(model.ListAccomodation)) { var attrAccomodations = model.ListAccomodation.Split(","); foreach (var item in attrAccomodations) { var yachtAttributeValue = new YachtAttributeValues { AttributeCategoryFid = (int)YachtAttributeEnum.Accomodations, AttributeFid = item.Split("_")[0].ToInt32(), AttributeValue = item.Split("_")[1], YachtFid = yacht.Id, LastModifiedBy = userId, LastModifiedDate = DateTime.Now }; listYachtAttrVal.Add(yachtAttributeValue); } } //Add yacht port if (model.PortLocationId > 0) { var portInfo = _context.PortLocations.AsNoTracking().Where(x => x.Id == model.PortLocationId && x.Deleted == false).FirstOrDefault(); if (portInfo != null) { var yachtPort = new YachtPorts { YachtFid = yacht.Id, PortFid = portInfo.Id, PortName = portInfo.PickupPointName, IsActivated = true, Deleted = false, CreatedBy = userId, CreatedDate = DateTime.Now, EffectiveDate = DateTime.Now }; //update location for yacht yacht.Country = portInfo.Country; yacht.City = portInfo.City; _context.Yachts.Update(yacht); _context.YachtPorts.Add(yachtPort); } } //Add yacht attributevalue amenities if (!string.IsNullOrEmpty(model.ListAmenities)) { var attrAmenities = model.ListAmenities.Split(","); foreach (var item in attrAmenities) { var yachtAttributeValue = new YachtAttributeValues { AttributeCategoryFid = (int)YachtAttributeEnum.Ametities, AttributeFid = item.Split("_")[0].ToInt32(), AttributeValue = item.Split("_")[1], YachtFid = yacht.Id, LastModifiedBy = userId, LastModifiedDate = DateTime.Now }; listYachtAttrVal.Add(yachtAttributeValue); } } if (listYachtAttrVal.Count > 0) { _context.YachtAttributeValues.AddRange(listYachtAttrVal); } //Add yacht non bussinessday if (!string.IsNullOrEmpty(model.ListNonBusinessDay)) { var listNonBizDay = model.ListNonBusinessDay.Split(","); var listYachtNonBizDay = new List <YachtNonOperationDays>(); foreach (var item in listNonBizDay) { var date = item.Replace(".", "-"); var dateTime = (date + "-" + DateTime.Now.Year).ToNullDateTime(); if (dateTime != null) { var yachtNonBizDay = new YachtNonOperationDays { YachtFid = yacht.Id, Recurring = false, Remark = "", StartDate = dateTime.Value, EndDate = dateTime.Value, CreatedBy = userId, CreatedDate = DateTime.Now, LastModifiedBy = userId, LastModifiedDate = DateTime.Now }; listYachtNonBizDay.Add(yachtNonBizDay); } } _context.YachtNonOperationDays.AddRange(listYachtNonBizDay); } //Add yachtCounter var yachtCounter = new YachtCounters { YachtId = yacht.Id, YachtUniqueId = yacht.UniqueId, TotalViews = 0, TotalBookings = 0, TotalSuccessBookings = 0, TotalReviews = 0, TotalRecommendeds = 0, TotalNotRecommendeds = 0 }; _context.YachtCounters.Add(yachtCounter); result = _context.SaveChanges(); transaction.Commit(); transaction.Dispose(); return(BaseResponse <bool> .Success()); } catch (Exception ex) { return(BaseResponse <bool> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } } }
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)); } } }