Ejemplo n.º 1
0
        /// <inheritdoc/>
        public async Task <HiveSection> UpdateHiveSectionAsync(int id, UpdateHiveSectionRequest updateRequest)
        {
            var dbHiveSections = await _context
                                 .Sections
                                 .Where(s => s.Code == updateRequest.Code && s.Id != id)
                                 .ToArrayAsync();

            if (dbHiveSections.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var dbHives = await _context.Hives.Where(h => h.Id == updateRequest.HiveId).ToArrayAsync();

            if (dbHives.Length == 0)
            {
                throw new RequestedResourceBadRequestException("hiveId");
            }

            dbHiveSections = await _context
                             .Sections
                             .Where(s => s.Id == id)
                             .ToArrayAsync();

            if (dbHiveSections.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbHiveSection = dbHiveSections[0];

            Mapper.Map(updateRequest, dbHiveSection);
            dbHiveSection.LastUpdatedBy = _userContext.UserId;

            await _context.SaveChangesAsync();

            return(Mapper.Map <HiveSection>(dbHiveSection));
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public async Task <HiveSection> CreateHiveSectionAsync(UpdateHiveSectionRequest createRequest)
        {
            var dbHiveSections =
                await _context
                .Sections
                .Where(p => p.Code == createRequest.Code)
                .ToArrayAsync();

            if (dbHiveSections.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var dbHiveSection = Mapper.Map <UpdateHiveSectionRequest, DbHiveSection>(createRequest);

            dbHiveSection.CreatedBy     = _userContext.UserId;
            dbHiveSection.LastUpdatedBy = _userContext.UserId;
            _context.Sections.Add(dbHiveSection);

            await _context.SaveChangesAsync();

            return(Mapper.Map <HiveSection>(dbHiveSection));
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        public async Task<HiveSection> UpdateHiveSectionAsync(int sectionId, UpdateHiveSectionRequest updateRequest)
        {
            var dbSections = await _context.Sections.Where(p => p.Code == updateRequest.Code && p.Id != sectionId).ToArrayAsync();
            if (dbSections.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbSections = await _context.Sections.Where(p => p.Id == sectionId).ToArrayAsync();
            if (dbSections.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbSection = dbSections[0];

            Mapper.Map(updateRequest, dbSection);
            dbSection.LastUpdatedBy = _userContext.UserId;

            await _context.SaveChangesAsync();

            return Mapper.Map<HiveSection>(dbSection);
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        public async Task <HiveSection> UpdateHiveSectionAsync(int hiveSectionId, UpdateHiveSectionRequest hiveSection)
        {
            var dbHivesSection = await _context.Sections.FirstOrDefaultAsync(p => p.Code == hiveSection.Code && p.Id != hiveSectionId).ConfigureAwait(false);

            if (dbHivesSection != null)
            {
                throw new RequestedResourceHasConflictException(Resources.HiveSectionCodeConflict);
            }

            dbHivesSection = await _context.Sections.FirstOrDefaultAsync(p => p.Id == hiveSectionId).ConfigureAwait(false);

            if (dbHivesSection == null)
            {
                throw new RequestedResourceNotFoundException(Resources.HiveSectionNotFound);
            }

            Mapper.Map(hiveSection, dbHivesSection);
            dbHivesSection.LastUpdatedBy = _userContext.UserId;

            await _context.SaveChangesAsync();

            return(Mapper.Map <HiveSection>(dbHivesSection));
        }
Ejemplo n.º 5
0
        /// <inheritdoc/>
        public async Task <HiveSection> CreateHiveSectionAsync(UpdateHiveSectionRequest createRequest)
        {
            var dbHiveSections = await _context.Sections.Where(s => s.Code == createRequest.Code).ToArrayAsync();

            if (dbHiveSections.Length > 0)
            {
                throw new RequestedResourceHasConflictException($"Conflict while searching hive sections in {nameof(CreateHiveSectionAsync)}");
            }

            var dbHiveSection = Mapper.Map <UpdateHiveSectionRequest, DbHiveSection>(createRequest);

            dbHiveSection.CreatedBy     = _userContext.UserId;
            dbHiveSection.LastUpdatedBy = _userContext.UserId;
            dbHiveSection.StoreHiveId   = createRequest.StoreHiveId;
            dbHiveSection.LastUpdated   = DateTime.UtcNow;
            dbHiveSection.Created       = DateTime.UtcNow;

            _context.Sections.Add(dbHiveSection);

            await _context.SaveChangesAsync();

            return(Mapper.Map <HiveSection>(dbHiveSection));
        }