예제 #1
0
        public ActionResult <QuestDto> CreateQuest(QuestForCreationDto quest)
        {
            //if(quest.Name==quest.Description)
            //{
            //    ModelState.AddModelError("Description",
            //        "Description must be different from Name.");
            //}
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var questEntity = _mapper.Map <Entities.Quest>(quest);
            var user        = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
            var userEntity  = _croudSeekRepository.GetUsers().Where((u) => u.Name == user).FirstOrDefault();

            if (userEntity != null)
            {
                questEntity.OwnerId = userEntity.Id;
            }
            _croudSeekRepository.AddQuest(questEntity);
            _croudSeekRepository.Save();

            var questToReturn = _mapper.Map <QuestDto>(questEntity);

            questToReturn.CanEdit = true;
            questToReturn.IsOwner = true;
            return(CreatedAtRoute("GetQuest",
                                  new { questId = questToReturn.Id },
                                  questToReturn));
        }
예제 #2
0
        public async Task <QuestDto> AddQuest(QuestForCreationDto quest)
        {
            var json = JsonSerializer.Serialize(quest);

            var questJson =
                new StringContent(json, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync("api/quests", questJson);

            if (response.IsSuccessStatusCode)
            {
                return(await JsonSerializer.DeserializeAsync <QuestDto>(await response.Content.ReadAsStreamAsync()));
            }

            return(null);
        }