示例#1
0
        public async Task <IActionResult> WatchUser(int id, int recipientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var watch = await _repo.GetWatch(id, recipientId);

            if (watch != null)
            {
                return(BadRequest("User already on Watch List"));
            }

            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }

            watch = new Watch
            {
                DoctorId  = id,
                PatientId = recipientId
            };

            _repo.Add(watch);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to add user to watch list"));
        }
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

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

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

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

            _repo.Add(message);


            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("GetMessage",
                                      new { userId, id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
        public IHttpActionResult Post(RecordDto record)
        {
            if (record == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var recordModel = record.ToModel();

            _recordsRepository.Add(recordModel);
            record.Id = recordModel.Id;

            return(Created(Url.Link("DefaultApi",
                                    new { controller = "Records", id = record.Id }), record));
        }