예제 #1
0
        public async Task <IActionResult> PutCity(int id, City city)
        {
            if (id != city.Idcity)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
예제 #2
0
        public IActionResult CreatePost([FromBody] CreatePostCommand model)
        {
            if (model.UserId == null)
            {
                return(BadRequest("UserId property cannot be null"));
            }
            if (model.GroupId != null)
            {
                var group = _context.Groups.Find(model.GroupId);
                if (group == null)
                {
                    return(NotFound("Could not find group with this id"));
                }
            }
            var post = _mapper.Map <Post>(model);

            post.DateTime = DateTime.Now.AddHours(2);
            if (!string.IsNullOrEmpty(model.MediaPath))
            {
                var mediaCheck = _context.Media.SingleOrDefaultAsync(p => p.MediaPath == model.MediaPath);
                if (!string.IsNullOrEmpty(mediaCheck.Result.MediaPath))
                {
                    if (_context.Media.Find(mediaCheck.Result.Idmedia) != null)
                    {
                        post.MediaId = mediaCheck.Result.Idmedia;
                    }
                    else
                    {
                        var media = new Medium
                        {
                            MediaPath = model.MediaPath
                        };
                        _context.Media.Add(media);
                        _context.SaveChanges();
                    }
                }
            }

            try
            {
                _context.Posts.Add(post);
                _context.SaveChangesAsync();
                return(Ok());
            }
            catch (ApplicationException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
        }
예제 #3
0
        public async Task <IActionResult> SendFriendRequest([FromRoute] int userRequestID, [FromRoute] int userRespondID)
        {
            if ((_context.Friends.Where(x => x.UserRequestId == userRequestID).Where(x => x.UserResponderId == userRespondID).Any()) ||
                (_context.Friends.Where(x => x.UserRequestId == userRespondID).Where(x => x.UserResponderId == userRequestID).Any()))
            {
                return(BadRequest("These users are already friends!"));
            }

            if (_context.Users.Find(userRequestID) == null)
            {
                return(BadRequest("User for this request Id does not exist"));
            }
            ;
            if (_context.Users.Find(userRespondID) == null)
            {
                return(BadRequest("User for this respond Id does not exist"));
            }
            ;
            if (((_context.FriendRequests.Where(x => x.UserRequestId == userRequestID).Where(x => x.UserResponderId == userRespondID).Any())) ||
                ((_context.FriendRequests.Where(x => x.UserRequestId == userRespondID).Where(x => x.UserResponderId == userRequestID).Any())))
            {
                return(BadRequest("Friend request for these users already exist!"));
            }
            ;

            var friendRequest = new FriendRequest
            {
                UserRequestId   = userRequestID,
                UserResponderId = userRespondID,
                DateTime        = DateTime.Now.AddHours(2)
            };

            var addedFriendRequest = _context.FriendRequests.Add(friendRequest);
            await _context.SaveChangesAsync();

            return(Ok());
        }
예제 #4
0
        public async Task <IActionResult> DeleteUser(int id)
        {
            var user = await _context.Users.FindAsync(id);

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

            user.FirstName = "Deleted";
            user.LastName  = "User";
            user.Birthday  = DateTime.Now;
            user.Email     = "no mail";
            user.CityId    = 8;
            await _context.SaveChangesAsync();

            return(Ok());
        }