public ActionResultStatus SetCamera(CameraInfo camera) { try { camera.IdUser = GetCurrentUserInfo().id; var cameraInfo = dBContext.CameraInfos.FirstOrDefault(x => x.IdUser == camera.IdUser); if (cameraInfo == null) { dBContext.CameraInfos.Add(camera); dBContext.SaveChanges(); return(ActionResultStatus.Success); } else { cameraInfo.Camera = camera.Camera; cameraInfo.CameraLens = camera.CameraLens; dBContext.SaveChanges(); return(ActionResultStatus.Success); } } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } }
public ActionResultStatus DeleteModerator(int userId) { try { var user = dBContext.Users.Find(userId); user.RoleId = Roles.User; dBContext.SaveChanges(); return(ActionResultStatus.Success); } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } }
public ActionResultStatus AddReport(int postId, ReportTypes reportType) { try { var report = new Report() { PostId = postId, UserId = GetCurrentUserInfo().id, ReportType = reportType, IsActive = true }; dBContext.Reports.Add(report); dBContext.SaveChanges(); return(ActionResultStatus.Success); } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } }
public PostRatingContext SetPostRating(int postId, int postRating) { try { var curUser = GetCurrentUserInfo(); var userRating = dBContext.Ratings.FirstOrDefault(x => x.IdUser == curUser.id && x.IdPost == postId); if (userRating != null) { userRating.RatingValue = postRating; dBContext.SaveChanges(); } else { var rating = new Rating() { IdUser = curUser.id.Value, IdPost = postId, RatingValue = postRating }; dBContext.Ratings.Add(rating); dBContext.SaveChanges(); } var postRatings = dBContext.Ratings.Where(x => x.IdPost == postId); var generalRating = Math.Round((double)postRatings.Sum(x => x.RatingValue) / (double)postRatings.Count(), 2, MidpointRounding.AwayFromZero); return(new PostRatingContext() { Status = Models.Statuses.ActionResultStatus.Success, CurUserRating = postRating, GeneralRating = generalRating }); } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(null); } }
public CommentInfo AddCommentToPost(CreateComment createComment) { try { var comment = new Comment() { UserId = GetCurrentUserInfo().id.Value, Date = DateTime.UtcNow, PostId = createComment.PostId, TextComment = createComment.TextComment }; dBContext.Comments.Add(comment); dBContext.SaveChanges(); return(Fill(comment)); } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(null); } }
public ActionResultStatus Register(Register registerUser) { try { var user = dBContext.Add(new User() { Email = registerUser.Email, UserName = registerUser.UserName, Password = GetHashPassword(registerUser.Password), RoleId = Roles.User, VerificationCode = GenerateVerificationCode() }); dBContext.SaveChanges(); mailSender.Send(user.Entity.Email, user.Entity.VerificationCode, user.Entity.Id); return(ActionResultStatus.Success); } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } }
public async Task <ActionResultStatus> AddPost(string paramPost, IFormFile file) { if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount)) { var upload = JsonConvert.DeserializeObject <UploadPhoto>(paramPost); upload.file = file; var curUser = GetCurrentUserInfo(); var time = DateTime.Now.ToString("dd.MM.yyyy_HH:mm:ss"); string namefile = $"{curUser.name}_{time}"; try { CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(config.Value.Container); CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(namefile); await blockBlob.UploadFromStreamAsync(upload.file.OpenReadStream()); string upload_url = blockBlob.Uri.AbsoluteUri; using var transaction = dBContext.Database.BeginTransaction(); try { Post post = new Post() { Id_User = curUser.id.Value, Title_post = upload.title_post, Description_post = upload.description_post, Url_photo = upload_url, PostTypeId = upload.postType }; dBContext.Posts.Add(post); dBContext.SaveChanges(); upload.photoParam.Id_post = post.Id; dBContext.PhotoParams.Add(upload.photoParam); dBContext.SaveChanges(); transaction.Commit(); } catch (Exception ex) { await blockBlob.DeleteAsync(); transaction.Rollback(); Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } } catch (Exception ex) { Logger.LogError(ex.Message, ex); return(ActionResultStatus.Error); } return(ActionResultStatus.Success); } return(ActionResultStatus.Error); }