예제 #1
0
        public async Task <BlTResult <View.Models.Scare> > CreateScareAsync(CreateScareRequest request, Monster monster)
        {
            Monsters.DAL.Models.Door door = await _repository.GetDoorAsync(request.DoorId).ConfigureAwait(false);

            if (door == null)
            {
                string errorMsg = $"Create scare failed. Door doesnt exist. Request: {request.ToString()} Monster: {monster.ToString()} ";
                _logger.LogError(errorMsg);
                return(new BlTResult <View.Models.Scare>()
                {
                    IsSuccess = false,
                    ErrorMsg = errorMsg,
                    StatusCode = StatusCodes.Status400BadRequest
                });
            }

            if (door.LastScare.Date >= DateTime.Today.Date)
            {
                string errorMsg = $"Create scare failed. Door already in use today by someone. Request: {request.ToString()} Monster: {monster.ToString()} ";
                _logger.LogError(errorMsg);
                return(new BlTResult <View.Models.Scare>()
                {
                    IsSuccess = false,
                    ErrorMsg = errorMsg,
                    StatusCode = StatusCodes.Status409Conflict
                });
            }

            try
            {
                door.LastScare = DateTime.Today;
                var updateResult = await _repository.UpdateDoorAsync(door).ConfigureAwait(false);

                Monsters.DAL.Models.Scare createResult = await _repository.CreateScareAsync(
                    new Monsters.DAL.Models.Scare()
                {
                    Id = Guid.NewGuid().ToString(), Door = door, MonsterId = monster.Id, ScareBegin = DateTime.Now
                }).ConfigureAwait(false);

                return(new BlTResult <View.Models.Scare>()
                {
                    IsSuccess = true,
                    Data = PrimitiveMapperHelper.Map(createResult)
                });
            }
            catch (Exception ex)
            {
                string errorMsg = $"Create scare failed. Request: {request.ToString()}. Monster: {monster.ToString()}. Error: {ex.ToString()}";
                _logger.LogError(errorMsg);
                return(new BlTResult <View.Models.Scare>()
                {
                    IsSuccess = false,
                    ErrorMsg = errorMsg,
                    StatusCode = StatusCodes.Status400BadRequest
                });
            }
        }
예제 #2
0
        public async Task <Monsters.DAL.Models.Door> UpdateDoorAsync(Monsters.DAL.Models.Door door)
        {
            if (door != null)
            {
                Context.Doors.Update(door);
                await Context.SaveChangesAsync();

                return(door);
            }

            return(null);
        }