public async Task <BaseResponse <bool> > CreateRangeAsync(List <YachtAttributeValuesCreateModel> modelCreate) { try { var createItems = new List <YachtAttributeValues>(); foreach (var item in modelCreate) { var createItem = new YachtAttributeValues(); createItem.InjectFrom(item); createItem.BasedAffective = false; createItem.LastModifiedBy = GetUserGuidId(); createItem.LastModifiedDate = DateTime.Now; createItems.Add(createItem); } if (createItems.Count() > 0) { await _context.YachtAttributeValues.AddRangeAsync(createItems); await _context.SaveChangesAsync(); return(BaseResponse <bool> .Success(true)); } return(BaseResponse <bool> .NoContent()); } catch (Exception ex) { return(BaseResponse <bool> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } }
public async Task <BaseResponse <bool> > CreateAsync(YachtAttributeValuesCreateModels modelCreate) { try { var createItem = new YachtAttributeValues(); createItem.InjectFrom(modelCreate); createItem.BasedAffective = false; createItem.LastModifiedBy = GetUserGuidId(); createItem.LastModifiedDate = DateTime.Now; await _context.YachtAttributeValues.AddAsync(createItem); await _context.SaveChangesAsync(); return(BaseResponse <bool> .Success()); } catch (Exception ex) { return(BaseResponse <bool> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } }
public async Task <BaseResponse <bool> > UpdateAttributeValueRangeAsync(YachtAttributeValuesUpdateRangeModel updateModel) { try { var userId = GetUserGuidId(); if (updateModel == null || updateModel.ListAttributeId == null || updateModel.ListAttributeValue == null) { return(BaseResponse <bool> .BadRequest()); } if (_context.YachtAttributeValues.AsNoTracking().Any(k => k.AttributeCategoryFid == updateModel.AttributeCategoryFid && k.YachtFid == updateModel.YachtFid)) { if (updateModel.ListAttributeId.Count() == 0 && updateModel.ListAttributeValue.Count() == 0) { var data = _context.YachtAttributeValues.Where(k => k.AttributeCategoryFid == updateModel.AttributeCategoryFid && k.YachtFid == updateModel.YachtFid); _context.RemoveRange(data); _context.SaveChanges(); return(BaseResponse <bool> .Success(true)); } else { // step 1 : remove all current attribute of Yacht in db var data = _context.YachtAttributeValues.Where(k => k.AttributeCategoryFid == updateModel.AttributeCategoryFid && k.YachtFid == updateModel.YachtFid); _context.RemoveRange(data); _context.SaveChanges(); // step 2: add new all attribute to db if (updateModel.ListAttributeId.Count() > 0 && updateModel.ListAttributeValue.Count() > 0 && updateModel.ListAttributeId.Count() == updateModel.ListAttributeValue.Count()) { var listAttributeId = updateModel.ListAttributeId; var listAtrributeValue = updateModel.ListAttributeValue; var lstattribute = new List <YachtAttributeValues>(); for (int i = 0; i < listAttributeId.Count; i++) { var attributeValue = new YachtAttributeValues() { YachtFid = updateModel.YachtFid, AttributeCategoryFid = updateModel.AttributeCategoryFid, AttributeFid = listAttributeId[i], AttributeValue = listAtrributeValue[i].ToString(), EffectiveDate = DateTime.Now, LastModifiedBy = userId, LastModifiedDate = DateTime.Now }; // add to list lstattribute.Add(attributeValue); } if (lstattribute.Count > 0) { await _context.YachtAttributeValues.AddRangeAsync(lstattribute); await _context.SaveChangesAsync(); return(BaseResponse <bool> .Success(true)); } } return(BaseResponse <bool> .NoContent(false)); } } else { if (updateModel.ListAttributeId.Count() > 0 && updateModel.ListAttributeValue.Count() > 0 && updateModel.ListAttributeId.Count() == updateModel.ListAttributeValue.Count()) { var listAttributeId = updateModel.ListAttributeId; var listAtrributeValue = updateModel.ListAttributeValue; var lstattribute = new List <YachtAttributeValues>(); for (int i = 0; i < listAttributeId.Count; i++) { var attributeValue = new YachtAttributeValues() { YachtFid = updateModel.YachtFid, AttributeCategoryFid = updateModel.AttributeCategoryFid, AttributeFid = listAttributeId[i], AttributeValue = listAtrributeValue[i].ToString(), EffectiveDate = DateTime.Now, LastModifiedBy = userId, LastModifiedDate = DateTime.Now }; // add to list lstattribute.Add(attributeValue); } if (lstattribute.Count > 0) { await _context.YachtAttributeValues.AddRangeAsync(lstattribute); await _context.SaveChangesAsync(); return(BaseResponse <bool> .Success(true)); } } return(BaseResponse <bool> .NoContent(false)); } } catch (Exception ex) { return(BaseResponse <bool> .InternalServerError(message : ex.Message, fullMsg : ex.StackTrace)); } }
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)); } } }