Esempio n. 1
0
        public async Task CreateAsync(Guid id, string name, string description, Guid userId)
        {
            var memo = await _memoRepository.GetByIdAsync(id);

            if (memo != null)
            {
                throw new Exception($"Memo with id: {id} already exists.");
            }
            memo = new Memo(id, name, description, userId);
            await _memoRepository.AddAsync(memo);
        }
Esempio n. 2
0
        [HttpPost] // @PostMapping
        public async Task <IActionResult> AddAsync([FromBody] Memo dto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            // <>
            var temp = new Memo();

            temp.Name    = dto.Name;
            temp.Title   = dto.Title;
            temp.Content = dto.Content;
            temp.Created = DateTime.UtcNow;
            // --TODO--
            // </>

            try
            {
                var model = await _repository.AddAsync(temp);

                if (model == null)
                {
                    return(BadRequest());
                }

                //[!] 다음 항목 중 원하는 방식 사용
                if (DateTime.Now.Second % 60 == 0)
                {
                    return(Ok(model)); // 200 OK
                }
                else if (DateTime.Now.Second % 3 == 0)
                {
                    return(CreatedAtRoute("GetMemoById", new { id = model.Id }, model)); // Status: 201 Created
                }
                else if (DateTime.Now.Second % 2 == 0)
                {
                    var uri = Url.Link("GetMemoById", new { id = model.Id });
                    return(Created(uri, model)); // 201 Created
                }
                else
                {
                    // GetById 액션 이름 사용해서 입력된 데이터 반환
                    return(CreatedAtAction(nameof(GetMemoById), new { id = model.Id }, model));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest());
            }
        }