Пример #1
0
        public async Task <ActionResult <Models.DTO.LabelDTO> > Post(Models.DTO.LabelDTO label)
        {
            if (label.Name == null)
            {
                return(BadRequest($"Label name not provided"));
            }

            if (_dbContext.Labels
                .AsEnumerable()
                .Any(l => string.Equals(l.Name, label.Name, StringComparison.CurrentCultureIgnoreCase)))
            {
                return(Conflict($"Label '{label.Name}' already exists."));
            }

            Models.Label newLabel = new Models.Label(label);
            newLabel.LabelId = 0;

            _dbContext.Labels.Add(newLabel);
            await _dbContext.SaveChangesAsync();

            return(CreatedAtAction(
                       nameof(Get),
                       new { id = newLabel.LabelId },
                       new Models.DTO.LabelDTO(newLabel)
                       ));
        }
        public async Task <ActionResult <Models.DTO.NoteDTO> > Post(Models.DTO.NoteDTO note)
        {
            Models.Note newNote = null;

            try
            {
                newNote = new Models.Note(note, _dbContext);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            // Update timestamps
            newNote.Created = DateTime.Now;
            newNote.Edited  = newNote.Created;

            _dbContext.Notes.Add(newNote);
            await _dbContext.SaveChangesAsync();

            return(CreatedAtAction(
                       nameof(Get),
                       new { id = newNote.NoteId },
                       new Models.DTO.NoteDTO(newNote)
                       ));
        }