public IActionResult CreateYacht(YachtCreateModel model) { var result = _yachtService.CreateYacht(model); if (result.IsSuccessStatusCode) { return(Ok(result)); } return(BadRequest()); }
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)); } } }