Beispiel #1
0
        public async Task <IActionResult> CreateEvent([FromBody] CreateEventDto createEventDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var eventToCreate = new Event
            {
                EventName        = createEventDto.EventName,
                EventDescription = createEventDto.EventDescription,
                EventOwnerId     = currentUserId,
            };

            _repo.Add(eventToCreate);

            if (await _repo.SaveAll())
            {
                return(StatusCode(201));
            }

            throw new Exception("Failed on save.");
        }
        public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDto messageForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDto.RecipientId);

            var sender = await _repo.GetUser(messageForCreationDto.SenderId);

            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);

            var messageToReturn = _mapper.Map <MessageForCreationDto>(message);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }