public async Task <IActionResult> AddProject(Project project) { User user = await _userManager.FindByNameAsync(User.Identity.Name); Project myProject = new Project() { ProjectManager = user, Links = project.Links, Description = project.Description, StatusProject = project.StatusProject, Name = project.Name, Team = new List <DeveloperProject>(), Vakancies = new List <Vakanci>() }; context.Projects.Add(myProject); await context.SaveChangesAsync(); user.Projects.Add(new DeveloperProject() { Project = myProject, User = user }); Project proj = context.Projects.Where(x => x.Name == project.Name).First(); AddAvatar(project.Avatar, proj); return(RedirectToAction("MainAccountProfile", "Account")); }
/// <summary> /// Добавление лайка /// </summary> /// <param name="context">Конект данных</param> /// <param name="user">Пользователь</param> /// <param name="id">Id страницы</param> /// <param name="status">К какому типу страницы относится лайк(Проект или Статья)</param> /// <returns></returns> public async Task <int?> AddLike(IndieContext context, User user, int id, LikeStatus status) { switch (status) { case LikeStatus.Project: Like like = context.Likes.Include(x => x.Project).Where(x => x.Project.ProjectID == id && x.User == user).FirstOrDefault(); Project project = context.Projects.Where(x => x.ProjectID == id).FirstOrDefault(); if (like == null) { Like newlike = new Like { Project = project, User = user }; context.Likes.Add(newlike); project.Likes.Add(newlike); await context.SaveChangesAsync(); return(context.Likes.Where(x => x.Project == project).Count()); } else { project.Likes.Remove(like); context.Likes.Remove(like); await context.SaveChangesAsync(); return(context.Likes.Where(x => x.Project == project).Count()); } case LikeStatus.Article: Like LikeArticle = context.Likes.Include(x => x.Article).Where(x => x.Article.ID == id && x.User == user).FirstOrDefault(); Article article = context.Articles.Where(x => x.ID == id).FirstOrDefault(); if (LikeArticle == null) { Like newlike = new Like { Article = article, User = user }; context.Likes.Add(newlike); article.Likes.Add(newlike); await context.SaveChangesAsync(); return(context.Likes.Where(x => x.Article == article).Count()); } else { article.Likes.Remove(LikeArticle); context.Likes.Remove(LikeArticle); await context.SaveChangesAsync(); return(context.Likes.Where(x => x.Article == article).Count()); } } return(null); }
public async Task <string> AddAvatar([FromBody] AvatarWithParameters parameter) { string base64 = parameter.Avatar.Remove(0, parameter.Avatar.IndexOf("base64") + 7); var bytes = Convert.FromBase64String(base64); MemoryStream mStream = new MemoryStream(); await mStream.WriteAsync(bytes, 0, Convert.ToInt32(bytes.Length)); User user = await _userManager.FindByNameAsync(User.Identity.Name); User Currentuser = context.Users.FirstOrDefault(x => x.Id == user.Id); string newPath = @"images/avatars/" + user.Id.ToString(); using (MagickImage image = new MagickImage(mStream)) { image.Resize(parameter.width, parameter.height); image.Crop(parameter.x1, parameter.y1, parameter.x2 - parameter.x1, parameter.y2 - parameter.y1); switch (image.Format) { case MagickFormat.Jpeg: image.Write(_appEnviroment.WebRootPath + "\\images\\avatars\\" + Currentuser.Id.ToString() + ".jpeg"); newPath += ".jpeg"; break; case MagickFormat.Jpg: image.Write(_appEnviroment.WebRootPath + "\\images\\avatars\\" + Currentuser.Id.ToString() + ".jpg"); newPath += ".jpg"; break; case MagickFormat.Png: image.Write(_appEnviroment.WebRootPath + "\\images\\avatars\\" + Currentuser.Id.ToString() + ".png"); newPath += ".png"; break; case MagickFormat.Bmp: image.Write(_appEnviroment.WebRootPath + "\\images\\avatars\\" + Currentuser.Id.ToString() + ".bmp"); newPath += ".bmp"; break; case MagickFormat.Gif: image.Write(_appEnviroment.WebRootPath + "\\images\\avatars\\" + Currentuser.Id.ToString() + ".gif"); newPath += ".gif"; break; } } mStream.Dispose(); Currentuser.Avatar = newPath; await context.SaveChangesAsync(); return("Успешно"); }
public async Task <IActionResult> AddCommentToArticle(string txt, int id) { User user = await _userManager.FindByNameAsync(User.Identity.Name); Article currentArticle = context.Articles.FirstOrDefault(x => x.ID == id); context.ArticleCommentaries.Add(new ArticleCommentaries { Content = txt, Author = user, DateSend = DateTime.Now, Article = currentArticle }); await context.SaveChangesAsync(); return(RedirectToAction("CurrentArticle/" + id.ToString(), "Home")); }