Beispiel #1
0
        public async Task <ActionResult> UpdateItem(Guid id, [FromBody] ItemUpdateDto itemDto)
        {
            var hasGroup = await GroupApplication.ExistGroup(g => g.Id.Equals(itemDto.GroupId));

            if (!hasGroup)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"O grupo, {itemDto.GroupId}, não existe.");
                return(BadRequest(error));
            }

            // Check if the item exists
            ItemResponseDto item = await ItemApplication.GetItem(i => i.Id.Equals(id));

            if (item == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado.");
                return(NotFound(error));
            }

            if (item.Name != itemDto.Name)
            {
                // Check if the item already exists
                ItemResponseDto itemByName = await ItemApplication.GetItem((i) => i.Name.ToLower().Equals(itemDto.Name.ToLower()));

                if (itemByName != null)
                {
                    ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"O item, {itemDto.Name}, já existe.");
                    return(BadRequest(error));
                }
            }

            await ItemApplication.UpdateItem(id, itemDto);

            return(Ok());
        }
Beispiel #2
0
        public async Task <ActionResult <ItemResponseDto> > GetItemById(Guid id)
        {
            ItemResponseDto itemDto = await ItemApplication.GetItem(i => i.Id.Equals(id));

            if (itemDto == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado.");
                return(NotFound(error));
            }

            return(Ok(itemDto));
        }
Beispiel #3
0
        public async Task <ActionResult> DeleteItem(Guid id)
        {
            // Check if the item exists
            ItemResponseDto itemDto = await ItemApplication.GetItem(i => i.Id.Equals(id));

            if (itemDto == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado.");
                return(NotFound(error));
            }

            await ItemApplication.DeleteItem(itemDto);

            return(NoContent());
        }
Beispiel #4
0
        public async Task <ActionResult> UpdateItemImage(Guid id, [FromForm] IFormFile photo)
        {
            // Check if the item exists
            ItemResponseDto item = await ItemApplication.GetItem(i => i.Id.Equals(id));

            if (item == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado.");
                return(NotFound(error));
            }

            await ItemApplication.UpdateItemImage(id, photo, Request);

            return(Ok());
        }
Beispiel #5
0
        public async Task DeleteItem(ItemResponseDto itemDto)
        {
            var item      = this.Mapper.Map <Item>(itemDto);
            var itemModel = this.Repository.GetAllAsQueryable().Where(e => e.Id.Equals(itemDto.Id)).First();

            if (!string.IsNullOrEmpty(itemModel.ImagePath))
            {
                var fileInfo = new FileInfo(itemModel.ImagePath);

                if (fileInfo.Exists)
                {
                    fileInfo.Delete();
                }
            }

            this.Repository.Delete(item);
            await this.Repository.SaveAsync();
        }
Beispiel #6
0
        public ResponseDto GetAvailableFlight(SearchInputDto searchInput)
        {
            List <ItemResponseDto> itemResponses   = new List <ItemResponseDto>();
            ItemResponseDto        itemResponseDto = new ItemResponseDto()
            {
                AvailableSeat    = 20,
                BookingAvailable = true,
                FlightName       = "SAA Mango",
                FlightScheduleId = 1
            };

            itemResponses.Add(itemResponseDto);
            ResponseDto responseDto = new ResponseDto
            {
                ItemResponses = itemResponses,
                Status        = 200,
                Error         = null
            };

            return(responseDto);
        }
        public ResponseDto GetAvailableFlight(SearchInputDto searchInput)
        {
            ResponseDto            response     = new ResponseDto();
            List <ItemResponseDto> responseItem = new List <ItemResponseDto>();

            try
            {
                foreach (var passenger in GetTotalPassengers())
                {
                    var query = from fs in _bookingDBContext.FlightSchedules
                                where
                                fs.FromDestination == searchInput.FromDestination &&
                                fs.ToDestination == searchInput.ToDestination &&
                                fs.DepatureDateTime == searchInput.DepatureDateTime &&
                                passenger.TotalPassengers < fs.Flight.Capacity
                                group new { fs.Flight, fs } by new
                    {
                        fs.Flight.FlightId,
                        fs.Flight.Name,
                        fs.FlightScheduleId,
                        fs.Flight.Capacity
                    } into g
                             select new
                    {
                        g.Key.FlightId,
                        g.Key.Name,
                        g.Key.FlightScheduleId,
                        TotalSeatLeft = (g.Key.Capacity - passenger.TotalPassengers)
                    };
                    foreach (var q in query)
                    {
                        bool bookingCheck = false;
                        if (searchInput.NoOfPassenger > q.TotalSeatLeft)
                        {
                            bookingCheck = false;
                        }
                        else
                        {
                            bookingCheck = true;
                        }
                        ItemResponseDto responseDto = new ItemResponseDto
                        {
                            AvailableSeat    = q.TotalSeatLeft,
                            FlightName       = q.Name,
                            BookingAvailable = bookingCheck,
                            FlightScheduleId = q.FlightScheduleId
                        };

                        responseItem.Add(responseDto);
                    }
                }
                response.ItemResponses = responseItem;
            }
            catch (Exception ex)
            {
                ErrorItem errorItem = new ErrorItem
                {
                    Code            = 400,
                    InternalMessage = ex.Message,
                    UserMessage     = "Error occurs while searching for available flights",
                    MethodName      = "GetAvailableFlight"
                };
                Error error = new Error
                {
                    Errors = errorItem
                };
                response.Error = error;
            }
            return(response);
        }