Advertisement
Guest User

Untitled

a guest
May 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.56 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7. namespace myapp
  8. {
  9.     public static class Helpers
  10.     {
  11.         public static MetricReport ToMetricReport(this object x)
  12.         {
  13.             throw new NotImplementedException();
  14.         }
  15.     }
  16.  
  17.     public class MetricReport
  18.     {
  19.         public string MetricData { get; set; }
  20.     }
  21.  
  22.     class Strategy
  23.     {
  24.         public Strategy()
  25.         {
  26.             userMetricsAlgorytms = new List<IUserMetricCalculator> {
  27.                 new TopVisitedMetric(),
  28.                 new AverageVisitedAgeMetric(),
  29.                 new PreferedGenderMetric()
  30.             };
  31.         }
  32.  
  33.         private List<IUserMetricCalculator> userMetricsAlgorytms;
  34.  
  35.         enum UserMetricType
  36.         {
  37.             TopVisited,
  38.             AverageVisitedAge,
  39.             PreferedGender
  40.         }
  41.  
  42.         interface IUserMetricCalculator
  43.         {
  44.             UserMetricType Type { get; }
  45.  
  46.             MetricReport Calculate(User user);
  47.         }
  48.  
  49.         class TopVisitedMetric : IUserMetricCalculator
  50.         {
  51.             public UserMetricType Type => UserMetricType.TopVisited;
  52.  
  53.             public MetricReport Calculate(User user)
  54.             {
  55.                 return user.VisitedUsers.GroupBy(x => x.Id).OrderBy(x => x.Count()).ToMetricReport();
  56.             }
  57.         }
  58.  
  59.         class AverageVisitedAgeMetric : IUserMetricCalculator
  60.         {
  61.             public UserMetricType Type => UserMetricType.AverageVisitedAge;
  62.  
  63.             public MetricReport Calculate(User user)
  64.             {
  65.                 return user.VisitedUsers.Average(x => x.Age).ToMetricReport();
  66.             }
  67.         }
  68.  
  69.         class PreferedGenderMetric : IUserMetricCalculator
  70.         {
  71.             public UserMetricType Type => UserMetricType.AverageVisitedAge;
  72.  
  73.             public MetricReport Calculate(User user)
  74.             {
  75.                 return user.VisitedUsers.GroupBy(x => x.Gender).Max(x => x.Count()).ToMetricReport();
  76.             }
  77.         }
  78.  
  79.         class User
  80.         {
  81.             public int Id { get; set; }
  82.             public string Name { get; set; }
  83.             public int Age { get; set; }
  84.             public IEnumerable<User> VisitedUsers { get; set; }
  85.             public bool Gender { get; internal set; }
  86.         }
  87.  
  88.         MetricReport GetUserMetric(User user, UserMetricType metricType)
  89.         {
  90.             return userMetricsAlgorytms.Single(x => x.Type == metricType).Calculate(user);
  91.         }
  92.  
  93.         // template
  94.         abstract class UserAction<TActionParams, TResult> where TResult : class
  95.         {
  96.             protected abstract bool CheckPermissions();
  97.  
  98.             protected abstract TResult DoAction(User Object, User Subject, TActionParams Params);
  99.  
  100.             public TResult Execute(User Object, User Subject, TActionParams Params)
  101.             {
  102.                 TResult result = null;
  103.                 if (CheckPermissions())
  104.                 {
  105.                     result = DoAction(Object, Subject, Params);
  106.                 }
  107.                 LogAction(Object, Subject, result);
  108.  
  109.                 return result;
  110.             }
  111.  
  112.             private void LogAction(User Object, User Subject, TResult Result)
  113.             {
  114.                 throw new NotImplementedException();
  115.             }
  116.         }
  117.  
  118.         class OpenUserPage : UserAction<User, UserPage>
  119.         {
  120.             protected override bool CheckPermissions()
  121.             {
  122.                 throw new NotImplementedException();
  123.             }
  124.  
  125.             protected override UserPage DoAction(User Object, User Subject, User Params)
  126.             {
  127.                 throw new NotImplementedException();
  128.             }
  129.         }
  130.  
  131.         class LikeUserPhoto : UserAction<UserPhoto, LikeResult>
  132.         {
  133.             protected override bool CheckPermissions()
  134.             {
  135.                 throw new NotImplementedException();
  136.             }
  137.  
  138.             protected override LikeResult DoAction(User Object, User Subject, UserPhoto Params)
  139.             {
  140.                 throw new NotImplementedException();
  141.             }
  142.         }
  143.  
  144.         //21.05
  145.         interface PostStatus
  146.         {
  147.             string Title { get; set; }
  148.  
  149.             string Description { get; set; }
  150.  
  151.             DateTime Date { get; set; }
  152.         }
  153.  
  154.         class NewStatus : PostStatus
  155.         {
  156.             public string Title { get; set; } = "New";
  157.  
  158.             public string Description { get; set; } = "Post created right now and waiting for moderation";
  159.  
  160.             public DateTime Date { get; set; } = DateTime.Now;
  161.         }
  162.  
  163.         class OnModerationStatus : PostStatus
  164.         {
  165.             public string Title { get; set; } = "On Moderation";
  166.  
  167.             public string Description { get; set; } = "Post was sended on moderation";
  168.  
  169.             public DateTime Date { get; set; } = DateTime.Now;
  170.         }
  171.  
  172.         class RejectedStatus : PostStatus
  173.         {
  174.             public string Title { get; set; } = "Post Rejected";
  175.  
  176.             public string Description { get; set; }
  177.  
  178.             public DateTime Date { get; set; } = DateTime.Now;
  179.         }
  180.  
  181.         class AprovedStatus : PostStatus
  182.         {
  183.             public string Title { get; set; } = "Post Aproved";
  184.  
  185.             public string Description { get; set; }
  186.  
  187.             public DateTime Date { get; set; } = DateTime.Now;
  188.         }
  189.  
  190.         class Post
  191.         {
  192.             private PostStatus _currentPostStatus;
  193.  
  194.             public PostStatus CurrentPostStatus
  195.             {
  196.                 get => _currentPostStatus;
  197.                 set
  198.                 {
  199.                     PostStatusesHistory.Add(_currentPostStatus);
  200.                     _currentPostStatus = value;
  201.                 }
  202.             }
  203.  
  204.             public List<PostStatus> PostStatusesHistory { get; set; }
  205.  
  206.             public string Text { get; set; }
  207.  
  208.             public string RejectReason { get; set; }
  209.         }
  210.  
  211.         class PostPublication : UserAction<Post, PostResult>
  212.         {
  213.             protected override bool CheckPermissions()
  214.             {
  215.                 throw new NotImplementedException();
  216.             }
  217.  
  218.             protected override PostResult DoAction(User Object, User Subject, Post post)
  219.             {
  220.                 if (post.CurrentPostStatus is RejectedStatus || post.CurrentPostStatus is NewStatus)
  221.                 {
  222.                     SendOnModeration(post);
  223.                     return new PostResult("Send on moderation");
  224.                 }
  225.                 else if (post.CurrentPostStatus is AprovedStatus)
  226.                 {
  227.                     Publicate(post);
  228.                     return new PostResult("Posted");
  229.                 }
  230.                 else
  231.                 {
  232.                     return new PostResult("Still on moderation");
  233.                 }
  234.             }
  235.  
  236.             private void Publicate(Post post)
  237.             {
  238.                 throw new NotImplementedException();
  239.             }
  240.  
  241.             private void SendOnModeration(Post post)
  242.             {
  243.                 post.CurrentPostStatus = new OnModerationStatus();
  244.             }
  245.         }
  246.         //21.05
  247.         // template end
  248.     }
  249.  
  250.     internal class ModerationResult
  251.     {
  252.     }
  253.  
  254.     internal class PostResult
  255.     {
  256.         public PostResult(string message)
  257.         {
  258.             Message = message;
  259.         }
  260.  
  261.         public string Message { get; set; }
  262.     }
  263.  
  264.     internal class UserPhoto
  265.     {
  266.     }
  267.     internal class LikeResult
  268.     {
  269.     }
  270.     internal class UserPage
  271.     {
  272.     }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement