Ejemplo n.º 1
0
        public async Task <bool> create(UserViewModel user)
        {
            await createValidation(user);

            try
            {
                User _user = new User();
                _user.id        = user.id;
                _user.name      = user.name;
                _user.isDeleted = false;
                if (user.hobbies != null && user.hobbies.Count > 0)
                {
                    _user.hobbies = user.hobbies.Select(c => new Hobbie
                    {
                        hobbie = c.hobbie,
                    }).ToList();
                }
                await db.AddAsync(_user);

                var result = await db.SaveChangesAsync();

                return(result > 0);
            }
            catch (Exception ex)
            {
                log.error("Create user error - {0}", ex: ex, user);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public virtual async Task <JsonResult> makeOrder()
        {
            var cart = await _context.Cart.Where(x => x.user_id == User.Identity.Name).ToListAsync();

            float sum = 0.0f;

            foreach (Cart item in cart)
            {
                sum += item.sum;
            }
            Orders order = new Orders();

            order.items = JsonConvert.SerializeObject(cart);
            order.summa = sum;
            await _context.AddAsync(order);

            _context.Cart.RemoveRange(cart);
            await _context.SaveChangesAsync();

            if (cart != null)
            {
                return(new JsonResult("Successfully added"));
            }
            else
            {
                return(new JsonResult("Error"));
            }
        }
        /// <summary>
        /// 创建任务流模板
        /// </summary>
        public async Task <string> CreateAsync(TaskTemplateDto templateDto, CurrentUserDto user)
        {
            if (templateDto.Name.IsBlank())
            {
                throw new Exception("模板名称不能为空");
            }
            if (templateDto.Id.IsNotBlank())
            {
                var template = await _context.TaskTemplates.FindAsync(templateDto.Id);

                if (template != null)
                {
                    template.Name = templateDto.Name;
                }
            }
            else
            {
                var template = new TaskTemplateEntity {
                    Name = templateDto.Name
                };
                template.CreateEntity(user.UserId);
                template.SetStep(TaskTemplateStep.Save);
                await _context.AddAsync(template);

                templateDto.Id = template.Id;
            }

            await _context.SaveChangesAsync();

            return(templateDto.Id);
        }
Ejemplo n.º 4
0
        private async Task BuildDefaultServices()
        {
            var categories = await DBContext.Categories.ToDictionaryAsync(c => c.Name, c => c.Id);

            foreach (var serviceConfig in Config.DefaultServices)
            {
                var match = await DBContext.Services.Include(s => s.Category).SingleOrDefaultAsync(s => s.Key == serviceConfig.Key);

                // Same key and Description, skip
                if (match != null && match.Description == serviceConfig.Description && match.Category.Name == serviceConfig.Category)
                {
                    continue;
                }

                // Same key but new description, update description
                if (match != null)
                {
                    match.Description = serviceConfig.Description;
                    match.CategoryId  = categories[serviceConfig.Category];
                    DBContext.Update(match);
                    continue;
                }

                // New entry, add to DB
                await DBContext.AddAsync(new ServiceEntity
                {
                    Key         = serviceConfig.Key,
                    Description = serviceConfig.Description,
                    CategoryId  = categories[serviceConfig.Category]
                });
            }

            await DBContext.SaveChangesAsync();
        }
Ejemplo n.º 5
0
        public virtual async Task <TEntity> CreateAsync(TEntity model)
        {
            await _dbContext.AddAsync <TEntity>(model);

            await _dbContext.SaveChangesAsync();

            return(model);
        }
Ejemplo n.º 6
0
        public async Task <RedirectResult> AuthorizeCallback(string code, string state)
        {
            if (string.IsNullOrEmpty(state) || !Memory.TryGetValue(state, out _))
            {
                // Unrecognized Nonce, go back home
                return(BackToHome());
            }

            // Delete the nonce now, we are done with it
            Memory.Remove(state);

            // Check if we already recognize this code, if not, build it
            var codeEntity = await DBContext.OAuthCodes.FindAsync(code);

            if (codeEntity == null)
            {
                codeEntity = new OAuthCodeEntity
                {
                    Value = code
                };
                await DBContext.AddAsync(codeEntity);

                await DBContext.SaveChangesAsync();
            }

            var token = string.IsNullOrEmpty(codeEntity.UserId) ? null :
                        DBContext
                        .OAuthTokens
                        .FirstOrDefault(t =>
                                        t.UserId == codeEntity.UserId && (t.Expiry == null || t.Expiry.Value > DateTime.Now)
                                        );

            // This user already has a valid access token, lets just use that instead
            if (token != null)
            {
                return(BackToHome(token));
            }

            // This is a new, unrecognized user, try and fetch an access token

            var tokenUri = UriPath.Combine(Config.PathOfExileApi.Route, "/OAuth/Token");

            var redirectUri = Url.Action("TokenCallback", "OAuth", null, Request.Scheme);
            var queryParams = new Dictionary <string, string>
            {
                { "client_id", Config.PathOfExileApi.ClientId },
                { "client_secret", Config.PathOfExileApi.ClientSecret },
                { "code", code },
                { "grant_type", "authorization_code" },
                { "redirect_uri", redirectUri }
            };

            var tokenQuery = QueryHelpers.AddQueryString(tokenUri, queryParams);

            return(Redirect(tokenQuery));
        }
Ejemplo n.º 7
0
        public virtual async Task <T> ADD(T addedItem)
        {
            if (addedItem == null)
            {
                return(null);
            }
            await db.AddAsync <T>(addedItem);

            await db.SaveChangesAsync();

            return(addedItem);
        }
Ejemplo n.º 8
0
        private async Task BuildDefaultCategories()
        {
            var categoryNames = await DBContext.Categories.Select(c => c.Name).ToListAsync();

            foreach (var name in Config.DefaultCategories.Except(categoryNames))
            {
                await DBContext.AddAsync(new ServiceCategoryEntity()
                {
                    Name = name
                });
            }

            await DBContext.SaveChangesAsync();
        }
Ejemplo n.º 9
0
        public async Task <RedirectResult> TokenCallback(string access_token, long?expires_in)
        {
            var token = await EnsureUserAsync(access_token);

            if (expires_in.HasValue)
            {
                token.Expiry = DateTime.Now.AddSeconds(expires_in.Value);
            }

            await DBContext.AddAsync(token);

            await DBContext.SaveChangesAsync();

            return(BackToHome(token));
        }
Ejemplo n.º 10
0
        public async Task <string> Insert_eva_cat_edificios(eva_cat_edificios eva_cat_edificios)
        {
            try
            {
                await DBLoContext.AddAsync(eva_cat_edificios);

                var res = await DBLoContext.SaveChangesAsync() > 0 ? "OK" : "ERROR AL REGISTRAR";

                DBLoContext.Entry(eva_cat_edificios).State = EntityState.Detached;
                return(res);
            }
            catch (Exception ex)
            {
                return(ex.Message.ToString());
            }
        }
Ejemplo n.º 11
0
        public async Task <TEntity> AgregarAsync(TEntity entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException($"{nameof(AgregarAsync)} entity must not be null");
            }
            try
            {
                await _db.AddAsync(entity);

                await _db.SaveChangesAsync();

                return(entity);
            }
            catch (System.Exception ex)
            {
                throw new Exception($"{nameof(entity)} could not be saved: {ex.Message}");
            }
        }
Ejemplo n.º 12
0
        public async Task FicMetInsertNewEdificio(Eva_cat_edificios FicMetInsertEdificio)
        {
            try
            {
                var FicSourceEdificioExist = await(
                    from edificios in LoDBContext.eva_cat_edificios
                    where edificios.IdEdificio == FicMetInsertEdificio.IdEdificio
                    select edificios
                    ).FirstOrDefaultAsync();

                if (FicSourceEdificioExist == null)
                {
                    FicMetInsertEdificio.FechaReg    = DateTime.Today;
                    FicMetInsertEdificio.FechaUltMod = DateTime.Today;
                    FicMetInsertEdificio.UsuarioReg  = "Brian Casas";
                    FicMetInsertEdificio.UsuarioMod  = "Brian Casas";
                    FicMetInsertEdificio.Activo      = "S";
                    FicMetInsertEdificio.Borrado     = "N";

                    await LoDBContext.AddAsync(FicMetInsertEdificio);
                }
                else
                {
                    FicMetInsertEdificio.IdEdificio  = FicSourceEdificioExist.IdEdificio;
                    FicMetInsertEdificio.FechaUltMod = DateTime.Today;
                    LoDBContext.Entry(FicSourceEdificioExist).State = EntityState.Detached;
                    LoDBContext.Update(FicMetInsertEdificio);
                }

                await LoDBContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                await new Page().DisplayAlert("ALERTA", e.Message.ToString(), "OK");
            }
        }//Insertar nuevo
Ejemplo n.º 13
0
        public virtual async Task InsertAsync(T entity)
        {
            await _context.AddAsync(entity);

            Commit();
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> Import(IFormFile file)
        {
            var actProds = await _db.Products.ToListAsync();

            FileStream     fs;
            List <Product> prods = new List <Product>();

            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

            //file load
            try
            {
                if (file == null)
                {
                    return(View("Index", new IndexProductModel {
                        Products = actProds, Error = 1
                    }));
                }
                if (file.Length <= 0)
                {
                    return(View("Index", new IndexProductModel {
                        Products = actProds, Error = 1
                    }));
                }
                var extension = Path.GetExtension(file.FileName);
                if (!extension.Contains("xls") && !extension.Contains("xlsx"))
                {
                    return(View("Index", new IndexProductModel {
                        Products = actProds, Error = 2
                    }));
                }
                var filePath = Path.GetTempFileName();
                fs = new FileStream(filePath, FileMode.Create);
                await file.CopyToAsync(fs);
            }
            catch { return(View("Index", new IndexProductModel {
                    Products = actProds, Error = 3
                })); }

            //load xls
            try
            {
                using (fs)
                {
                    using (var reader = ExcelReaderFactory.CreateReader(fs))
                    {
                        int indexCode   = 0;
                        int indexName   = 1;
                        int indexVat    = 2;
                        int indexSellPr = 3;
                        int indxAvrPr   = 4;
                        int vatd        = 22;

                        if (reader.FieldCount < 5)
                        {
                            return(View("Index", new IndexProductModel {
                                Products = actProds, Error = 4
                            }));
                        }

                        while (reader.Read()) //Each row of the file
                        {
                            //salto righe vuote
                            var c0 = reader.GetValue(indexCode);
                            var c1 = reader.GetValue(indexName);
                            var c2 = reader.GetValue(indexVat);
                            var c3 = reader.GetValue(indexSellPr);
                            var c4 = reader.GetValue(indxAvrPr);
                            if (c0 == null || c1 == null || c2 == null || c3 == null || c4 == null)
                            {
                                continue;
                            }
                            //check validate row
                            if (!validateExcelRow(c0, c1, c2, c3, c4))
                            {
                                continue;
                            }
                            int    v2;
                            double v3, v4;
                            //vat
                            if (string.IsNullOrEmpty(c2.ToString()))
                            {
                                v2 = vatd;
                            }
                            else
                            {
                                int tmp;
                                var res = int.TryParse(c2.ToString(), out tmp);
                                v2 = (res) ? tmp : vatd;
                            }
                            //sell price
                            if (string.IsNullOrEmpty(c3.ToString()))
                            {
                                v3 = 0;
                            }
                            else
                            {
                                double tmp;
                                var    res = double.TryParse(c3.ToString(), out tmp);
                                v3 = (res) ? tmp : 0;
                            }
                            //aver price
                            if (string.IsNullOrEmpty(c4.ToString()))
                            {
                                v4 = 0;
                            }
                            else
                            {
                                double tmp;
                                var    res = double.TryParse(c4.ToString(), out tmp);
                                v4 = (res) ? tmp : 0;
                            }
                            var prod = new Product {
                                Code = c0.ToString(), Name = c1.ToString(), VAT = v2, SellingPrice = v3, AveragePrice = v4
                            };
                            prods.Add(prod);
                        }
                    }
                }
            }
            catch { return(View("Index", new IndexProductModel {
                    Products = actProds, Error = 4
                })); }

            //update database
            var actCodes = actProds.Select(p => p.Code).ToList();
            var actNames = actProds.Select(p => p.Name).ToList();

            foreach (var rProd in prods)
            {
                if (actCodes.Contains(rProd.Code) || actNames.Contains(rProd.Name))
                {
                    continue;
                }
                await _db.AddAsync(rProd);
            }
            await _db.SaveChangesAsync();

            return(View("Index", new IndexProductModel {
                Products = actProds
            }));
        }