Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace myapp
- {
- public static class Helpers
- {
- public static MetricReport ToMetricReport(this object x)
- {
- throw new NotImplementedException();
- }
- }
- public class MetricReport
- {
- public string MetricData { get; set; }
- }
- class Strategy
- {
- public Strategy()
- {
- userMetricsAlgorytms = new List<IUserMetricCalculator> {
- new TopVisitedMetric(),
- new AverageVisitedAgeMetric(),
- new PreferedGenderMetric()
- };
- }
- private List<IUserMetricCalculator> userMetricsAlgorytms;
- enum UserMetricType
- {
- TopVisited,
- AverageVisitedAge,
- PreferedGender
- }
- interface IUserMetricCalculator
- {
- UserMetricType Type { get; }
- MetricReport Calculate(User user);
- }
- class TopVisitedMetric : IUserMetricCalculator
- {
- public UserMetricType Type => UserMetricType.TopVisited;
- public MetricReport Calculate(User user)
- {
- return user.VisitedUsers.GroupBy(x => x.Id).OrderBy(x => x.Count()).ToMetricReport();
- }
- }
- class AverageVisitedAgeMetric : IUserMetricCalculator
- {
- public UserMetricType Type => UserMetricType.AverageVisitedAge;
- public MetricReport Calculate(User user)
- {
- return user.VisitedUsers.Average(x => x.Age).ToMetricReport();
- }
- }
- class PreferedGenderMetric : IUserMetricCalculator
- {
- public UserMetricType Type => UserMetricType.AverageVisitedAge;
- public MetricReport Calculate(User user)
- {
- return user.VisitedUsers.GroupBy(x => x.Gender).Max(x => x.Count()).ToMetricReport();
- }
- }
- class User
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public int Age { get; set; }
- public IEnumerable<User> VisitedUsers { get; set; }
- public bool Gender { get; internal set; }
- }
- MetricReport GetUserMetric(User user, UserMetricType metricType)
- {
- return userMetricsAlgorytms.Single(x => x.Type == metricType).Calculate(user);
- }
- // template
- abstract class UserAction<TActionParams, TResult> where TResult : class
- {
- protected abstract bool CheckPermissions();
- protected abstract TResult DoAction(User Object, User Subject, TActionParams Params);
- public TResult Execute(User Object, User Subject, TActionParams Params)
- {
- TResult result = null;
- if (CheckPermissions())
- {
- result = DoAction(Object, Subject, Params);
- }
- LogAction(Object, Subject, result);
- return result;
- }
- private void LogAction(User Object, User Subject, TResult Result)
- {
- throw new NotImplementedException();
- }
- }
- class OpenUserPage : UserAction<User, UserPage>
- {
- protected override bool CheckPermissions()
- {
- throw new NotImplementedException();
- }
- protected override UserPage DoAction(User Object, User Subject, User Params)
- {
- throw new NotImplementedException();
- }
- }
- class LikeUserPhoto : UserAction<UserPhoto, LikeResult>
- {
- protected override bool CheckPermissions()
- {
- throw new NotImplementedException();
- }
- protected override LikeResult DoAction(User Object, User Subject, UserPhoto Params)
- {
- throw new NotImplementedException();
- }
- }
- //21.05
- interface PostStatus
- {
- string Title { get; set; }
- string Description { get; set; }
- DateTime Date { get; set; }
- }
- class NewStatus : PostStatus
- {
- public string Title { get; set; } = "New";
- public string Description { get; set; } = "Post created right now and waiting for moderation";
- public DateTime Date { get; set; } = DateTime.Now;
- }
- class OnModerationStatus : PostStatus
- {
- public string Title { get; set; } = "On Moderation";
- public string Description { get; set; } = "Post was sended on moderation";
- public DateTime Date { get; set; } = DateTime.Now;
- }
- class RejectedStatus : PostStatus
- {
- public string Title { get; set; } = "Post Rejected";
- public string Description { get; set; }
- public DateTime Date { get; set; } = DateTime.Now;
- }
- class AprovedStatus : PostStatus
- {
- public string Title { get; set; } = "Post Aproved";
- public string Description { get; set; }
- public DateTime Date { get; set; } = DateTime.Now;
- }
- class Post
- {
- private PostStatus _currentPostStatus;
- public PostStatus CurrentPostStatus
- {
- get => _currentPostStatus;
- set
- {
- PostStatusesHistory.Add(_currentPostStatus);
- _currentPostStatus = value;
- }
- }
- public List<PostStatus> PostStatusesHistory { get; set; }
- public string Text { get; set; }
- public string RejectReason { get; set; }
- }
- class PostPublication : UserAction<Post, PostResult>
- {
- protected override bool CheckPermissions()
- {
- throw new NotImplementedException();
- }
- protected override PostResult DoAction(User Object, User Subject, Post post)
- {
- if (post.CurrentPostStatus is RejectedStatus || post.CurrentPostStatus is NewStatus)
- {
- SendOnModeration(post);
- return new PostResult("Send on moderation");
- }
- else if (post.CurrentPostStatus is AprovedStatus)
- {
- Publicate(post);
- return new PostResult("Posted");
- }
- else
- {
- return new PostResult("Still on moderation");
- }
- }
- private void Publicate(Post post)
- {
- throw new NotImplementedException();
- }
- private void SendOnModeration(Post post)
- {
- post.CurrentPostStatus = new OnModerationStatus();
- }
- }
- //21.05
- // template end
- }
- internal class ModerationResult
- {
- }
- internal class PostResult
- {
- public PostResult(string message)
- {
- Message = message;
- }
- public string Message { get; set; }
- }
- internal class UserPhoto
- {
- }
- internal class LikeResult
- {
- }
- internal class UserPage
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement