コード例 #1
0
        public async Task <IActionResult> PutAmenity(int id, AmenityDto amenityDto)
        {
            if (id != amenityDto.Id)
            {
                return(BadRequest());
            }

            try
            {
                await _amenity.UpdateAmenity(amenityDto);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AmenityExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #2
0
        public async void CanCreateAmenityAndSaveToDatabase()
        {
            DbContextOptions <AsyncInnDbContext> options = new DbContextOptionsBuilder <AsyncInnDbContext>()
                                                           .UseInMemoryDatabase("CanCreateAmenityAndSaveToDatabase")
                                                           .Options;

            using AsyncInnDbContext context = new AsyncInnDbContext(options);

            AmenityManager service = new AmenityManager(context);

            var dto = new AmenityDto()
            {
                Name = "Test"
            };

            Assert.Equal(0, context.Amenities.CountAsync().Result);

            var result = await service.CreateAmenity(dto);

            var actual = context.Amenities.FindAsync(result.Id).Result;

            Assert.Equal(1, await context.Amenities.CountAsync());
            Assert.IsType <Amenity>(actual);
            Assert.Equal(1, actual.Id);
            Assert.Equal("Test", actual.Name);
        }
コード例 #3
0
        public void Execute(AmenityDto dto)
        {
            this.createAmenityValidation.ValidateAndThrow(dto);

            var amenity = this.mapper.Map <Amenity>(dto);

            this.context.Amenities.Add(amenity);
            this.context.SaveChanges();
        }
コード例 #4
0
        public IActionResult Put(int id,
                                 [FromBody] AmenityDto dto,
                                 [FromServices] IEditAmenityCommand editAmenityCommand)
        {
            dto.Id = id;
            _dispatcher.DispatchCommand(editAmenityCommand, dto);

            return(NoContent());
        }
コード例 #5
0
        /// <summary>
        /// Updates a transfer object containing an amenity information
        /// </summary>
        /// <param name="dto">Data transfer object containing an amenity information to be updated</param>
        public async Task UpdateAmenity(AmenityDto dto)
        {
            var amenity = await _context.Amenities.FindAsync(dto.Id);

            amenity.Name = dto.Name;

            _context.Entry(amenity).State = EntityState.Modified;

            await _context.SaveChangesAsync();
        }
コード例 #6
0
        /// <summary>
        /// Deletes an Ammenity
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public async Task DeleteAmenity(int ID)
        {
            AmenityDto amenityDto = await GetAmenity(ID);

            Amenity amenity = new Amenity {
                ID = amenityDto.ID
            };

            _context.Remove(amenity).State = EntityState.Deleted;
            await _context.SaveChangesAsync();
        }
コード例 #7
0
        /// <summary>
        /// Get a data transfer object containing an amenity information
        /// </summary>
        /// <param name="id">Id of the amenity to be read</param>
        /// <returns>Data transfer object containig an amenity information</returns>
        public async Task <AmenityDto> GetAmenity(int id)
        {
            var amenity = await _context.Amenities.FindAsync(id);

            AmenityDto dto = new AmenityDto()
            {
                Id   = amenity.Id,
                Name = amenity.Name
            };

            return(dto);
        }
コード例 #8
0
        /// <summary>
        /// Creates a New Ammenity
        /// </summary>
        /// <param name="inboundAmenity"></param>
        /// <returns></returns>
        public async Task <Amenity> Create(AmenityDto inboundAmenity)
        {
            Amenity amenity = new Amenity
            {
                Name = inboundAmenity.Name
            };

            _context.Entry(amenity).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(amenity);
        }
コード例 #9
0
        public void Execute(AmenityDto dto)
        {
            this.editAmenityValidation.ValidateAndThrow(dto);

            var amenity = this.context.Amenities.Find(dto.Id);

            if (amenity == null)
            {
                throw new EntityNotFoundException(dto.Id);
            }

            this.mapper.Map(dto, amenity);

            this.context.SaveChanges();
        }
コード例 #10
0
        /// <summary>
        /// Creates an amenity entry in the database
        /// </summary>
        /// <param name="dto">Data transfer object containing an amenity properties to be created</param>
        /// <returns>Data transfer object with an Id</returns>
        public async Task <AmenityDto> CreateAmenity(AmenityDto dto)
        {
            Amenity amenity = new Amenity()
            {
                Name = dto.Name
            };

            _context.Entry(amenity).State = EntityState.Added;

            await _context.SaveChangesAsync();

            dto.Id = amenity.Id;

            return(dto);
        }
コード例 #11
0
    public async Task<ActionResult<AmenityDto>> PostAmenity(AmenityDto amenity)
    {
      Amenity newAmenity = await _amenity.Create(amenity);

      return CreatedAtAction("GetAmenity", new { id = amenity.ID }, amenity);
    }
コード例 #12
0
        public async Task <ActionResult <AmenityDto> > PostAmenity(AmenityDto amenityDto)
        {
            await _amenity.CreateAmenity(amenityDto);

            return(CreatedAtAction("GetAmenity", new { id = amenityDto.Id }, amenityDto));
        }
コード例 #13
0
 public IActionResult Post([FromBody] AmenityDto amenityDto,
                           [FromServices] ICreateAmenityCommand createAmenityCommand)
 {
     _dispatcher.DispatchCommand(createAmenityCommand, amenityDto);
     return(StatusCode(StatusCodes.Status201Created));
 }