public async Task <IActionResult> PutData([FromRoute] int id, [FromBody] Message data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != data.Id)
            {
                return(BadRequest());
            }

            _context.Entry(data).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DataExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> PutMessage(int id, Message message)
        {
            if (id != message.MessageId)
            {
                return(BadRequest());
            }

            _context.Entry(message).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MessageExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> UpdateMessage(int id, MessageDto messageDto)
        {
            if (id != messageDto.Id)
            {
                return(BadRequest());
            }

            //_context.Entry(messageDto).State = EntityState.Modified;


            var message = await _context.Messages.FindAsync(id);

            if (message == null)
            {
                return(NotFound());
            }

            message.UpdateFrom(messageDto);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MessageExists(id))
                {
                    return(NotFound());
                }
                throw;
            }

            return(NoContent());
        }
        public async Task <ActionResult <Message> > Create([FromBody] Message message)
        {
            if (string.IsNullOrEmpty(message.Content))
            {
                return(BadRequest());
            }

            var existingMessage = await _dbContext.Messages.FindAsync(message.Id);

            if (existingMessage != null)
            {
                return(Conflict());
            }

            _dbContext.Messages.Add(message);
            await _dbContext.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = message.Id }, message));
        }
        public async Task <bool> Add(string senderId, string SenderUsername, string title, string content)
        {
            var message = new Data.Models.Message
            {
                Id             = Guid.NewGuid().ToString(),
                SenderId       = senderId,
                SenderUsername = SenderUsername,
                Created        = DateTime.UtcNow,
                Title          = title,
                Content        = content,
                IsProcessed    = false,
                IsDeleted      = false
            };

            context.Add(message);
            var result = await context.SaveChangesAsync();

            return(result != 0);
        }
        public async Task <IActionResult> SendMessage([FromBody] MessageModel request)
        {
            if (ModelState.IsValid)
            {
                var message = new Message()
                {
                    Id       = Guid.NewGuid(),
                    Username = request.Username,
                    Text     = request.Text
                };

                _context.Messages.Add(message);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Example #7
0
        public async Task <ActionResult> Create(MessageCreatedBindingModel messageCreatedBindingModel)
        {
            Message message = new Message
            {
                Content   = messageCreatedBindingModel.Content,
                Username  = messageCreatedBindingModel.User,
                CreatedOn = DateTime.UtcNow
            };

            await context.Messages.AddAsync(message);

            await context.SaveChangesAsync();

            return(this.Ok());
        }
Example #8
0
        public async Task <ActionResult <Message> > SendMessage(Message message)
        {
            _context.Messages.Add(message);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (MessageExists(message.ID))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetMessage", new { id = message.ID }, message));
        }