Exemplo n.º 1
0
        /// <summary>
        /// Updates the properties of an Industry.
        /// </summary>
        /// <param name="id">The ID of the target Industry.</param>
        /// <param name="name">The new Industry name.</param>
        /// <param name="imgUrl">The new Industry image path.</param>
        /// <returns></returns>
        public async Task <IndustryModel> UpdateIndustry(int id, string name, string imgUrl)
        {
            if (await _context.Industries.AnyAsync(i => i.Name == name && i.Id != id))
            {
                throw new ArgumentException($"Industry with name {name} already exists.");
            }
            var industry = await _context.Industries
                           .Include(i => i.Reports)
                           .Include(i => i.Reports)
                           .ThenInclude(r => r.Author)
                           .FirstOrDefaultAsync(i => i.Id == id);

            ValidateIndustryExists(industry);
            if (name != null && name != string.Empty)
            {
                industry.Name = name;
            }
            if (imgUrl != null && imgUrl != string.Empty)
            {
                industry.ImgUrl = imgUrl;
            }
            industry.ModifiedOn = DateTime.UtcNow;
            await _context.SaveChangesAsync();

            var dto = IndustryMapper.MapModelFromEntity(industry);

            return(dto);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Lists all soft-deleted Industries from the context.
        /// </summary>
        /// <param name="search">Searches the Industries by name.</param>
        /// <returns>ICollection of Industry Models.</returns>
        public async Task <ICollection <IndustryModel> > GetDeletedIndustries(string search)
        {
            var industries = await _context.Industries
                             .Where(i => i.IsDeleted)
                             .Include(i => i.Reports)
                             .Select(i => IndustryMapper.MapModelFromEntity(i))
                             .ToListAsync();

            industries = SearchIndustries(search, industries).ToList();

            return(industries);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets an Industry by its ID.
        /// </summary>
        /// <param name="id">The ID of the target Industry.</param>
        /// <returns>IndustryModel on success. Throws ArgumentNullException if Industry doesn't exist.</returns>
        public async Task <IndustryModel> GetIndustry(int id)
        {
            var industry = await _context.Industries
                           .Include(i => i.SubscribedUsers)
                           .ThenInclude(ui => ui.User)
                           .Include(i => i.Reports)
                           .ThenInclude(r => r.Author)
                           .FirstOrDefaultAsync(i => i.Id == id);

            ValidateIndustryExists(industry);

            var dto = IndustryMapper.MapModelFromEntity(industry);

            return(dto);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Lists all active Industries. Industry must not be soft-deleted.
        /// </summary>
        /// <param name="sort">The property to sort the list by. Can be left blank for default sort by ID.</param>
        /// <param name="search">Searches Industries by name.</param>
        /// <returns>ICollection of Industry Models</returns>
        public async Task <ICollection <IndustryModel> > GetAllIndustries(string sort, string search)
        {
            var industries = await _context.Industries
                             .Where(i => !i.IsDeleted)
                             .Include(i => i.SubscribedUsers)
                             .ThenInclude(ui => ui.User)
                             .Include(i => i.Reports)
                             .ThenInclude(r => r.Author)
                             .Select(i => IndustryMapper.MapModelFromEntity(i))
                             .ToListAsync();

            industries = SortIndustries(sort, industries).ToList();
            industries = SearchIndustries(search, industries).ToList();

            return(industries);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets all the Industry Subscriptions for a User.
        /// </summary>
        /// <param name="userId">The target User ID.</param>
        /// <returns>ICollection of Industry Models.</returns>
        public async Task <ICollection <IndustryModel> > GetSubscriptions(int userId)
        {
            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);

            ValidateUserExists(user);

            var industries = await _context.IndustrySubscriptions
                             .Include(ui => ui.Industry)
                             .ThenInclude(i => i.SubscribedUsers)
                             .ThenInclude(ui => ui.User)
                             .Where(ui => ui.UserId == userId)
                             .Select(ui => IndustryMapper.MapModelFromEntity(ui.Industry))
                             .ToListAsync();

            return(industries);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new Industry in the context.
        /// </summary>
        /// <param name="name">The Name of the new Industry.</param>
        /// <param name="imgUrl">Link to the Image to be displayed in the new Industry's page.</param>
        /// <returns>IndustryModel on success. Throws ArgumentException if Industry already exists.</returns>
        public async Task <IndustryModel> CreateIndustry(string name, string imgUrl)
        {
            if (name == null)
            {
                throw new ArgumentNullException("Name can NOT be null.");
            }
            var dto = IndustryMapper.MapModelFromInput(name, imgUrl);

            if (await _context.Industries.AnyAsync(i => i.Name == name))
            {
                throw new ArgumentException($"Industry with name {name} already exists.");
            }
            var industry = IndustryMapper.MapEntityFromModel(dto);
            await _context.Industries.AddAsync(industry);

            await _context.SaveChangesAsync();

            dto = IndustryMapper.MapModelFromEntity(industry);
            return(dto);
        }