public ConfirmationResultViewModel ConfirmUser(string code, int userId) { var model = new ConfirmationResultViewModel(); model.Token = null; model.Confirmed = false; var exist = FindActiveCodeForUser(code, userId); if (exist == null) { model.Status = ConfirmationResults.CodeNotFound.ToString(); return(model); } var young = CheckCodeDate(code, userId); if (!young) { model.Status = ConfirmationResults.CodeExpired.ToString(); return(model); } ConfirmCode(exist.Id); model.Token = Guid.NewGuid(); var user = _raterPriceContext.Users.Where(u => u.Id == userId).Select(u => u).First(); user.Token = (Guid)model.Token; user.ConfirmationDate = DateTime.UtcNow; _raterPriceContext.SaveChanges(); model.Confirmed = true; model.Status = ConfirmationResults.Success.ToString(); return(model); }
public Price AddGoodPrice(AddPriceModel price) { var domain = new Price { DateUpdated = DateTime.UtcNow, GoodId = price.GoodId, PriceValue = price.PriceValue, ShopId = price.ShopId }; //domain.UserId= var existing = _raterPriceContext.Prices.Where(p => p.ShopId == price.ShopId && p.GoodId == price.GoodId) .Select(p => p) .FirstOrDefault(); if (existing == null) { _raterPriceContext.Prices.Add(domain); } else { existing.PriceValue = price.PriceValue; existing.DateUpdated = DateTime.UtcNow; } _raterPriceContext.SaveChanges(); return(existing); }
private static List <City> ImrportCities(string[] shops) { var citiesList = shops.Select(shop => shop.Split(';')[5]).ToList(); var groupedList = citiesList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <City>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var gr in groupedList) { var domainItem = new City() { Name = gr.Trim() }; if (!context.Cities.Any(g => g.Name.Equals(domainItem.Name))) { domainItem.Id = context.Cities.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("City has been imported: " + domainItem.Name); } else { domainItem.Id = context.Cities.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }
private static List <Weekday> ImportWeekDays() { var days = Enum.GetValues(typeof(DaysOfWeek)).Cast <DaysOfWeek>().ToList(); var domainList = days.Select(d => new Weekday() { Id = (int)d, Name = d.ToString() }).ToList(); domainList = domainList.OrderBy(d => d.Id).ToList(); foreach (var d in domainList) { using (RaterPriceContext context = RaterPriceContext.Create()) { if (context.Weekdays.Any(da => da.Name == d.Name)) { continue; } d.Id = context.Weekdays.Add(d).Id; context.SaveChanges(); Console.WriteLine("Working day has been imported: " + d.Name); } } return(domainList); }
public SendSmsResult CreateAndSendSms(string message, User user) { SmsSender sender; var smsMessage = new SmsMessage() { Text = message }; var send = new SmsMessageSend(); sender = _raterPriceContext.SmsSenders.Where(s => s.Id == smsSenderId).Select(s => s).First(); _raterPriceContext.SmsMessages.Add(smsMessage); _raterPriceContext.SaveChanges(); send.ServiceId = _smsGateWay.GetType().ToString(); send.SmsSenderId = sender.Id; send.SmsMessageId = smsMessage.Id; send.UserId = user.Id; send.CreationDate = null; send.StatusChangeDate = null; send.Status = null; _raterPriceContext.SmsMessageSends.Add(send); _raterPriceContext.SaveChanges(); var sendResult = _smsGateWay.SendSms(new List <string>() { user.PhoneNumber }, smsMessage.Text, send.Id, sender.Name); var updatingSend = _raterPriceContext.SmsMessageSends.Where(s => s.Id == send.Id).Select(s => s).First(); updatingSend.ResponseToSendSmsRequest = sendResult.RequestResult.TextView; if (sendResult.RequestResult.ErrorCode != null) { updatingSend.ErrorCode = sendResult.RequestResult.ErrorCode; } else { updatingSend.CreationDate = DateTime.UtcNow; } _raterPriceContext.SaveChanges(); return(sendResult); }
static void Main(string[] args) { var connectionString = ConfigurationManager.ConnectionStrings["raterPriceConnectionString"].ConnectionString; using (RaterPriceContext context = RaterPriceContext.Create()) { var goodsIdsWithPrices = context.Prices.Select(p => p.GoodId); var goodsWithoutPrice = context.Goods.Where(g => !goodsIdsWithPrices.Contains(g.Id)).Select(g => g).AsNoTracking().ToList(); if (!goodsWithoutPrice.Any()) { goodsWithoutPrice = context.Goods.Select(g => g).AsNoTracking().ToList(); } foreach (var good in goodsWithoutPrice) { var shopsIdsWithPrices = context.Prices.Select(p => p.ShopId); var shops = context.Shops.Where(s => !shopsIdsWithPrices.Contains(s.Id)).Select(s => s); if (shops.Count() < 2) { shops = context.Shops.Select(s => s); } var r1 = new Random(); var take = r1.Next(1, shops.Count() - 1); var skip = r1.Next(0, take - 1); var workingRange = shops.OrderBy(s => s.Id).Skip(() => skip).Take(() => take).AsNoTracking().ToList(); foreach (var shop in workingRange) { if (context.Prices.Any(p => p.ShopId == shop.Id && p.GoodId == good.Id)) { continue; } var r = new Random(); var price = new Price() { GoodId = good.Id, ShopId = shop.Id, DateUpdated = DateTime.UtcNow, PriceValue = Convert.ToDecimal(r.Next(1, 1000) + r.NextDouble()), UserId = r.Next() }; context.Prices.Add(price); context.SaveChanges(); Console.WriteLine(price.PriceValue + "->" + shop.Name + "->" + good.Name); } } } }
private static List <Shop> ImportShops(string[] shopsInStrings, List <Weekday> weekdays, List <PaymentType> paymentTypes, List <ShopGroup> groups, List <City> cities) { var domainShops = new List <Shop>(); foreach (var shopStr in shopsInStrings) { var domainShop = MapOneShopFromString(shopStr, groups, cities); using (RaterPriceContext context = RaterPriceContext.Create()) { domainShop.Id = context.Shops.Add(domainShop).Id; context.SaveChanges(); var paymentTypesForShop = GetPaymentTypesForShop(shopStr, paymentTypes, domainShop.Id); context.ShopPaymentTypes.AddRange(paymentTypesForShop); context.SaveChanges(); var shopWorkingDays = GetShopWeekdays(shopStr, weekdays, domainShop.Id); context.ShopWeekdays.AddRange(shopWorkingDays); context.SaveChanges(); } domainShops.Add(domainShop); Console.WriteLine("Shop has been imported: " + domainShop.Name); } return(domainShops); }
public User CreateSimpleUser(RegisterUserViewModel viewModel) { var user = MapUserToDomainModel(viewModel); _raterPriceContext.Users.Add(user); //_raterPriceContext.SaveChanges(); // var role = new UserRole // { // UserId = user.Id, // RoleId = _raterPriceContext.UserRoles.Where(r => r.Name.Equals("User")).Select(r => r.Id).First() // }; //_raterPriceContext.UserInRoles.Add(role); _raterPriceContext.SaveChanges(); return(user); }
private static List <ShopGroup> ImrportGroups(string[] shops) { var groupsList = new List <string>(); foreach (var shop in shops) { var groupsForOneItem1 = shop.Split(';')[2]; var groupsForOneItem2 = shop.Split(';')[3]; groupsList.Add(groupsForOneItem1); groupsList.Add(groupsForOneItem2); } var groupedList = groupsList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <ShopGroup>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var gr in groupedList) { var domainItem = new ShopGroup() { Name = gr.Trim() }; if (!context.ShopGroups.Any(g => g.Name.Equals(domainItem.Name))) { domainItem.Id = context.ShopGroups.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("Group has been imported: " + domainItem.Name); } else { domainItem.Id = context.ShopGroups.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }
private static List <PaymentType> ImrportPaymentTypes(string[] shops) { var paymentTypeList = new List <string>(); foreach (var shop in shops) { var paymentTypesForOneItem = shop.Split(';')[30].Split(','); paymentTypeList.AddRange(paymentTypesForOneItem); } var groupedList = paymentTypeList.GroupBy(x => x).Select(x => x.Key).ToList(); groupedList = groupedList.Where(g => !string.IsNullOrWhiteSpace(g)).Select(s => s).ToList(); var domainList = new List <PaymentType>(); using (RaterPriceContext context = RaterPriceContext.Create()) { foreach (var pt in groupedList) { var domainItem = new PaymentType() { Name = pt.Trim() }; if (!context.PaymentTypes.Any(p => p.Name.Equals(domainItem.Name))) { domainItem.Id = context.PaymentTypes.Add(domainItem).Id; context.SaveChanges(); Console.WriteLine("Payment type has been imported: " + domainItem.Name); } else { domainItem.Id = context.PaymentTypes.Where(p => p.Name.Equals(domainItem.Name)).Select(p => p.Id).First(); } domainList.Add(domainItem); } } return(domainList); }