public bool AddCity(City city) { if (!_context.Cities.Any(a => a.Name == city.Name)) { _context.Cities.Add(city); _context.SaveChanges(); return(true); } else { return(false); } }
public async Task <ActionResult <UserDto> > VerifyUser([FromRoute] int id) { List <User> users = await _context.Users.Include(x => x.Gender).Include(x => x.UserType).Include(x => x.City).ToListAsync(); var user = users.Find(x => x.Iduser == id); if (user == null || user.Verification == true) { return(NotFound()); } else { user.Verification = true; _context.Users.Update(user); _context.SaveChanges(); user.PasswordHash = null; return(await Task.FromResult(Ok(_mapper.Map <UserDto>(user)))); } }
public ActionResult ReportComment([FromBody] ReportCommand model) { if (_context.Users.Find(model.SenderId) == null) { return(NotFound("Could not find sender user with this id")); } if (_context.Comments.Find(model.ReportObjectId) == null) { return(NotFound("Could not find comment with this id")); } var commentReport = new CommentReport(); commentReport.Reason = model.Reason; commentReport.Explanation = model.Explanation; commentReport.DateTime = DateTime.Now.AddHours(2); commentReport.CommentId = model.ReportObjectId; commentReport.SenderId = model.SenderId; try { _context.CommentReports.Add(commentReport); _context.SaveChanges(); return(Ok()); } catch (Exception) { return(BadRequest("An error occured while trying to add comment report.")); } }
public User Create(User user, string password) { // validation if (string.IsNullOrWhiteSpace(password)) { throw new ApplicationException("Password is required"); } if (_context.Users.Any(x => x.Email == user.Email)) { throw new ApplicationException("Email \"" + user.Email + "\" is already taken"); } byte[] passwordHash = System.Text.Encoding.UTF8.GetBytes(SecurePasswordHasher.Hash(password)); user.PasswordHash = passwordHash; _context.Users.Add(user); _context.SaveChanges(); return(user); }
public ActionResult<GroupDto> CreateGroup([FromBody] CreateGroupCommand model) { if (string.IsNullOrEmpty(model.Name)) { return BadRequest("Name property cannot be null"); } var group = _mapper.Map<Group>(model); group.DateTime = DateTime.Now.AddHours(2); var user = _context.Users.Find(model.CreatorUserId); if(user == null) { return NotFound("User with provided CreatorUserId does not exist"); } try { _context.Groups.Add(group); var userGroup = new UserGroup(); userGroup.UserType = 1; userGroup.UserId = user.Iduser; _context.SaveChanges(); userGroup.GroupId = group.Idgroup; _context.UserGroups.Add(userGroup); _context.SaveChanges(); var groupDto = new GroupDto(); groupDto.IDGroup = group.Idgroup; groupDto.Name = group.Name; groupDto.Decription = group.Description; groupDto.CreatedDateTime = group.DateTime; return Ok(groupDto); } catch (ApplicationException ex) { return BadRequest(new { message = ex.Message }); } }
public ActionResult SendMessage([FromBody] SendMessageCommand model) { var allUsers = _context.Users.ToList(); if (!string.IsNullOrEmpty(model.SenderFirstLastName) && !string.IsNullOrEmpty(model.ReceiverFirstLastname)) { var allUsersNamesAndIds = new Dictionary <int, string>(); foreach (var user in allUsers) { string fullName = user.FirstName + ' ' + user.LastName; allUsersNamesAndIds.Add(user.Iduser, fullName); } var userList = allUsersNamesAndIds.ToList(); bool foundSender = false; bool foundReceiver = false; foreach (var item in userList) { if (item.Value == model.SenderFirstLastName) { model.SenderId = item.Key; foundSender = true; } if (item.Value == model.ReceiverFirstLastname) { model.ReceiverId = item.Key; foundReceiver = true; } } if (!foundSender) { return(NotFound("Could not find user with this sender full name")); } if (!foundReceiver) { return(NotFound("Could not find user with this receiver full name")); } } try { var userSender = _context.Users.Find(model.SenderId); } catch (Exception) { return(NotFound("Could not find user with this sender id")); } try { var userReceiver = _context.Users.Find(model.ReceiverId); } catch (Exception) { return(NotFound("Could not find user with this receiver id")); } if (string.IsNullOrEmpty(model.Text)) { return(BadRequest("Message text cannot be empty")); } var message = new Message(); message.DateTime = DateTime.Now.AddHours(2); message.Text = model.Text; message.SenderId = model.SenderId; message.ReceiverId = model.ReceiverId; try { _context.Messages.Add(message); _context.SaveChanges(); return(Ok()); } catch (ApplicationException ex) { return(BadRequest(new { message = ex.Message })); } }
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 })); } }