Ejemplo n.º 1
0
        public async Task <Result <SearchCriteriaDb> > AddAsync(SearchCriteriaDb criteriaDb)
        {
            try
            {
                List <CourseCategoryDb> courseCategories = new List <CourseCategoryDb>();
                List <DomainDb>         domains          = new List <DomainDb>();
                CourseCategoryDb        categoryDb;
                DomainDb domainDb;

                foreach (var el in criteriaDb.CourseCategories)
                {
                    categoryDb = context.CourseCategories
                                 .FirstOrDefault(x => x.Name == el.Name && x.Title == el.Title);
                    courseCategories.Add(categoryDb ??
                                         new CourseCategoryDb()
                    {
                        Name  = el.Name,
                        Title = el.Title
                    });
                }

                foreach (var el in criteriaDb.Domains)
                {
                    domainDb = context.Domains
                               .FirstOrDefault(x => x.DomainURL == el.DomainURL);
                    domains.Add(domainDb ??
                                new DomainDb()
                    {
                        DomainURL  = el.DomainURL,
                        DomainName = el.DomainName
                    });
                }
                criteriaDb.CourseCategories = courseCategories;
                criteriaDb.Domains          = domains;


                context.SearchCriterias.Add(criteriaDb);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(Result <SearchCriteriaDb> .Ok(criteriaDb));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(Result <SearchCriteriaDb> .Fail <SearchCriteriaDb>($"Cannot save SearchCriteria. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return(Result <SearchCriteriaDb> .Fail <SearchCriteriaDb>($"Cannot save SearchCriteria. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return(Result <SearchCriteriaDb> .Fail <SearchCriteriaDb>($"Invalid SearchCriteria. {e.Message}"));
            }
        }
        public async Task <Result <CourseCategoryDb> > AddAsync(CourseCategoryDb category)
        {
            try
            {
                context.CourseCategories.Add(category);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(Result <CourseCategoryDb> .Ok(category));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Cannot save category. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return((Result <CourseCategoryDb>) Result <CourseCategoryDb> .Fail <CourseCategoryDb>($"Invalid category. {e.Message + "/n" + e.EntityValidationErrors}"));
            }
        }
Ejemplo n.º 3
0
        public async Task <Result <DomainDb> > AddAsync(DomainDb domain)
        {
            try
            {
                context.Domains.Add(domain);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(Result <DomainDb> .Ok(domain));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return((Result <DomainDb>) Result <DomainDb> .Fail <DomainDb>($"Cannot save domain. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return((Result <DomainDb>) Result <DomainDb> .Fail <DomainDb>($"Cannot save domain. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return((Result <DomainDb>) Result <DomainDb> .Fail <DomainDb>($"Invalid domain. {e.Message}"));
            }
        }
Ejemplo n.º 4
0
        public async Task <Result <CourseBookmarkDb> > AddAsync(CourseBookmarkDb bookmarkDb)
        {
            try
            {
                context.CourseBookmarks.Add(bookmarkDb);
                await context.SaveChangesAsync();

                return(Result <CourseBookmarkDb> .Ok(bookmarkDb));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(Result <CourseBookmarkDb> .Fail <CourseBookmarkDb>($"Cannot save bookmark. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return(Result <CourseBookmarkDb> .Fail <CourseBookmarkDb>($"Cannot save bookmark. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return(Result <CourseBookmarkDb> .Fail <CourseBookmarkDb>($"Invalid bookmark. {e.Message}"));
            }
        }
Ejemplo n.º 5
0
        public async Task <User> Register(User user, string password)
        {
            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(password, out passwordHash, out passwordSalt);

            user.PasswordHash = passwordHash;
            user.PasswordSalt = passwordSalt;

            await _context.Users.AddAsync(user);

            await _context.SaveChangesAsync();

            return(user);
        }
        public async Task <Result <IEnumerable <CourseDb> > > AddRangeAsync(IEnumerable <CourseDb> coursesDb)
        {
            try
            {
                List <CourseDb>  listAddCourses    = new List <CourseDb>();
                List <CourseDb>  listUpdateCourses = new List <CourseDb>();
                CourseDb         courseUpd;
                DomainDb         domain;
                CourseCategoryDb category;
                foreach (var courseNew in coursesDb)
                {
                    courseUpd = context.Courses
                                .FirstOrDefault(x => x.URL == courseNew.URL);
                    if (courseUpd == null)
                    {
                        courseNew.Id = Guid.NewGuid().ToString();
                        domain       = context.Domains.FirstOrDefault(x => x.DomainURL == courseNew.Domain.DomainURL);
                        if (domain != null)
                        {
                            courseNew.Domain = domain;
                        }
                        else
                        {
                            courseNew.Domain = await CreateDomainDbAsync(courseNew.Domain);
                        }

                        category = context.CourseCategories.FirstOrDefault(x => x.Name == courseNew.Category.Name);
                        if (category != null)
                        {
                            courseNew.Category = category;
                        }
                        else
                        {
                            courseNew.Category = await CreateCategoryDbAsync(courseNew.Category);
                        }

                        listAddCourses.Add(courseNew);
                    }
                    else
                    {
                        if (courseUpd.OldPrice != courseNew.Price)
                        {
                            courseUpd.DateOldPrice = DateTime.Now;
                            courseUpd.OldPrice     = courseUpd.Price;
                            courseUpd.Price        = courseNew.Price;
                            courseUpd.Discount     = courseNew.Discount;
                        }
                        courseUpd.DateStartCourse = courseNew.DateStartCourse;
                        if (courseNew.Category != null && courseUpd.Category != null &&
                            courseUpd.Category.Name != courseNew.Category.Name)
                        {
                            courseUpd.Category = courseNew.Category;
                        }
                        courseUpd.Description = courseNew.Description;
                        if (courseNew.Domain != null && courseUpd.Domain != null &&
                            courseUpd.Domain.DomainURL != courseNew.Domain.DomainURL)
                        {
                            courseUpd.Domain = courseNew.Domain;
                        }
                        courseUpd.DateChange           = DateTime.Now;
                        context.Entry(courseUpd).State = EntityState.Modified;
                        await context.SaveChangesAsync().ConfigureAwait(false);
                    }
                }

                context.Courses.AddRange(listAddCourses);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(Result <IEnumerable <CourseDb> > .Ok(coursesDb));
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(Result <IEnumerable <CourseDb> > .Fail <IEnumerable <CourseDb> >($"Cannot save course. {e.Message}"));
            }
            catch (DbUpdateException e)
            {
                return((Result <IEnumerable <CourseDb> >) Result <IEnumerable <CourseDb> > .Fail <IEnumerable <CourseDb> >($"Cannot save course. Duplicate field. {e.Message}"));
            }
            catch (DbEntityValidationException e)
            {
                return((Result <IEnumerable <CourseDb> >) Result <IEnumerable <CourseDb> > .Fail <IEnumerable <CourseDb> >($"Invalid course. {e.Message}"));
            }
        }
Ejemplo n.º 7
0
 public async Task <bool> SaveAll()
 {
     return(await _context.SaveChangesAsync() > 0);
 }